合并字符串。计算原始字符串中有多少个指数(来自列表)。Python

2024-06-20 15:04:56 发布

您现在位置:Python中文网/ 问答频道 /正文

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个字符串的列表(序号1-3)。我将它们组合起来创建一个长字符串(组合) 我有一个索引(连接)列表。我为每个字符串设置了3个不同的计数器(计数1-3)

我要做的是找出组合序列中每个连接点[2,9,15,20]的位置。如果来自序列1-->;计数1+=1,如果来自序列2-->;计数2+=1,则来自序列3-->;计数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

如果我有什么需要澄清的,请告诉我


Tags: 字符串ingtindexsocount序列be
3条回答

你可以尝试一些基本的东西,比如

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

您可以在这里使用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

可以使用itertools中的collections.Counter、和repeatchain,例如:

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})

相关问题 更多 >