2013-06-25 43 views
0
junctions = [2,9,15,20] 

seq_1 = 'sauron' 
seq_2 = 'corrupted' 
seq_3 = 'numenor' 
combined = 'sauroncorruptednumenor' #seq_1 + seq_2 + seq_3 

count_1 = 1 
count_2 = 1 
count_3 = 2 

我有一個3個字符串(seq_1-3)的列表。我結合他們創建1個長字符串(合併) 我有一個索引列表(路口)。我有3個不同的計數器爲每個字符串設置爲零(count_1-3)組合字符串。計算原始字符串中有多少個索引(來自列表)。 Python

我想要做的是找到組合序列中每個交點[2,9,15,20]的位置。 。 。如果是從seq_1 - > COUNT_1 + = 1,如果是從seq_2 - > COUNT_2 + = 1,從seq_3 - > COUNT_3 + = 1

例如

junctions = [2,9,15,20] 
count_1 = 0 
count_2 = 0 
count_3 = 0 
combined = 'sauroncorruptednumenor' 
seq_1 = 'sauron' #index 2 would be on 'u' in combined but originally from seq_1 so count_1 = count_1 + 1 
seq_2 = 'corrupted' #index 9 would be on 'r' in combined so count_2 += 1 
seq_3 = 'numenor' #index 15 would be 'n' in combined so count_3 += 1, and 20 would be 'o' so count_3 += 1 

讓我知道如果我需要澄清任何不同

+0

如果我理解正確,你試圖檢查交叉口在哪個字開始? – 2rs2ts

+0

沒關係。我看到這些「路口」不是切片。 – 2rs2ts

回答

1

你可以嘗試一些基本的東西像

L_1 = len(seq_1) 
L_2 = len(seq_2) 
L_3 = len(seq_3) 

junctions = [2, 9, 15, 20] 
c_1, c_2, c_3 = (0, 0, 0) 

for j in junctions: 
    if j < L_1: 
     c_1 += 1 
    elif j < L_1 + L_2: 
     c_2 += 1 
    elif j < L_1 + L_2 + L_3: 
     c_3 += 1 
    else: 
     Raise error 
1

您可以使用collections.Counterbisect.bisect_left這裏:

>>> from collections import Counter 
>>> import bisect 
>>> junctions = [2,9,15,20] 
>>> seq_1 = 'sauron' 
>>> seq_2 = 'corrupted' 
>>> seq_3 = 'numenor' 
>>> lis = [seq_1, seq_2, seq_3] 

創建一個包含在其中每個seq_結束索引列表:

>>> start = -1 
>>> break_points = [] 
for item in lis: 
    start += len(item) 
    break_points.append(start) 
...  
>>> break_points 
[5, 14, 21] 

現在,我們可以簡單地遍歷junctions,發現每個路口的使用bisect.bisect_left功能break_points列表中的位置。

>>> Counter(bisect.bisect_left(break_points, jun)+1 for jun in junctions) 
Counter({3: 2, 1: 1, 2: 1}) 

更好的輸出使用collections.defaultdict

>>> from collections import defaultdict 
>>> dic = defaultdict(int) 
for junc in junctions: 
    ind = bisect.bisect_left(break_points, junc) +1 
    dic['count_'+str(ind)] += 1 
...  
>>> dic 
defaultdict(<type 'int'>, 
{'count_3': 2, 
'count_2': 1, 
'count_1': 1}) 

#accessing these counts 
>>> dic['count_3'] 
2 
0

能使用collections.Counter,並repeat和itertools chain,如:

from itertools import chain, repeat 
from operator import itemgetter 
from collections import Counter 

junctions = [2,9,15,20] 
seq_1 = 'sauron' 
seq_2 = 'corrupted' 
seq_3 = 'numenor' 

indices = list(chain.from_iterable(repeat(i, len(j)) for i, j in enumerate([seq_1, seq_2, seq_3], start=1))) 
print Counter(itemgetter(*junctions)(indices)) 
# Counter({3: 2, 1: 1, 2: 1}) 
相關問題