3
seq = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

start = [2,9,18]
end = [6,12,20]

#output = seq[end[k]:start[k+1]]

我正在尝试 end[k] 和 start[k+1] #seq[end[k]:start[k+1]] 之间的序列

所以在这种情况下应该有2个序列。

outp1 = seq[6:9] #'GHI'

out2 = seq[12:18] #'MNOPQR'
4

2 回答 2

5

使用zip

>>> seq = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> start = [2,9,18]
>>> end = [6,12,20]
>>> for x,y in zip(end, start[1:]):
...     print seq[x:y]
...     
GHI
MNOPQR

内存高效版本:

>>> from itertools import izip,islice
>>> for x,y in izip(end,islice(start,1,None)):
    print seq[x:y]
...     
GHI
MNOPQR
于 2013-07-10T18:28:56.293 回答
3
for i in range(len(end)-1):
    seq[end[i]:start[i+1]]

answer:-
    'GHI'
    'MNOPQR'
于 2013-07-10T18:42:43.563 回答