Negative array indicies and slice()

Roy Smith roy at panix.com
Mon Oct 29 09:52:50 EDT 2012


In article <mailman.3009.1351516065.27098.python-list at python.org>,
 Andrew Robinson <andrew3 at r3dsolutions.com> wrote:

> Show me an example where someone would write a slice with a negative and 
> a positive index (both in the same slice);
> and have that slice grab a contiguous slice in the *middle* of the list 
> with orientation of lower index to greater index.

It's possible in bioinformatics.  Many organisms have circular 
chromosomes.  It's a single DNA molecule spliced into a loop.  There's 
an "origin", but it's more a convenience thing for people to assign some 
particular base-pair to be location 0.  From the organism's point of 
view, the origin isn't anything special (but there *is* a fixed 
orientation).

It's entirely plausible for somebody to want to extract the sub-sequence 
from 100 bp (base-pairs) before the origin to 100 bp after the origin.  
If you were storing the sequence in Python string (or list), the most 
convenient way to express this would be seq[-100:100].  Likewise, if you 
wanted the *other* fragment, you would write seq[100:-100].

There is a minor confounding factor here in that biologists number 
sequences starting with 1, not 0.  At least that was the way when I was 
doing this stuff mumble years ago.  I don't know what the current 
convention is.



More information about the Python-list mailing list