<p>您可以在这里使用<code>collections.Counter</code>和<code>bisect.bisect_left</code>:</p>
<pre><code>>>> 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]
</code></pre>
<p>创建一个包含索引的列表,在每个<code>seq_</code>结束时:</p>
<pre><code>>>> start = -1
>>> break_points = []
for item in lis:
start += len(item)
break_points.append(start)
...
>>> break_points
[5, 14, 21]
</code></pre>
<p>现在我们可以简单地在<code>junctions</code>上循环,并使用<code>bisect.bisect_left</code>函数在<code>break_points</code>列表中找到每个连接点的位置。你知道吗</p>
<pre><code>>>> Counter(bisect.bisect_left(break_points, jun)+1 for jun in junctions)
Counter({3: 2, 1: 1, 2: 1})
</code></pre>
<p>更好的输出使用<code>collections.defaultdict</code>:</p>
<pre><code>>>> 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
</code></pre>