[Tutor] Getting range of a list

Hs Hs ilhs_hs at yahoo.com
Tue Feb 5 22:08:08 CET 2013



Dear List members:

I always have problem in getting ranges:

Following is my representation of part of my file.

>X1
A
G
C
G
>X2
A
G
>X3
A
G
>X4
H
T


I want to print the above contents in the following way:

X1 \t A
X1 \t G
X1 \t C
X1 \t G
X2 \t A
X2 \t G
X3 \t A
X3 \t G
X4 \t H
X4 \t H


Here is what I do :
>>> f1 = open('test','r')
>>> da = f1.read().split('\n')
>>> dat = da[:-1]
>>> dat
>>> mpos = []

>>> for i in range(len(dat)):
if dat[i].startswith('>'):
mpos.append(i)

>>> mpos
[0, 3, 6, 9]

>>> for item in range(len(mpos)):
start = mpos[item]
enda = item+1
end  = mpos[enda]-1
head = dat[start]
block  = dat[start+1:end]
for i in block:
print head+'\t'+i

>X1A
>X2A
>X3A

Traceback (most recent call last):
  File "<pyshell#31>", line 4, in <module>
    end  = mpos[enda]-1
IndexError: list index out of range
>>> 


By the time I am looping through last item, I do not have anything to take 1 from and thus end up with that indexerror. 

Could anyone please help me how to get a good habit of making this work.  This is a very wrong and bad habit. 

thank you for your help in advance. 

Hs.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130205/2ba1f956/attachment-0001.html>


More information about the Tutor mailing list