<p>有重复的数字似乎不合逻辑,但如果您想获得每行的匹配数字的计数,而不考虑索引,则将nums设为一个集合,并将每行中的一个数字的次数相加:</p>
<pre><code>from itertools import islice, imap
import csv
with open("in.txt") as f,open("numbers.txt") as nums:
# make a set of all winning nums
nums = set(imap(str.rstrip, nums))
r = csv.reader(f)
# iterate over each row and sum how many matches we get
for row in r:
print("{} matched {}".format(row[0], sum(n in nums
for n in islice(row, 1, None))))
</code></pre>
<p>使用您的输入将输出:</p>
^{pr2}$
<p>假设您的文件是逗号分隔的,并且您的数字文件中每行都有一个数字。在</p>
<p>如果您真的想知道哪些数字(如果有的话)存在,那么您需要迭代该数字并打印我们集合中的每个数字:</p>
<pre><code>from itertools import islice, imap
import csv
with open("in.txt") as f, open("numbers.txt") as nums:
nums = set(imap(str.rstrip, nums))
r = csv.reader(f)
for row in r:
for n in islice(row, 1, None):
if n in nums:
print("{} is in row {}".format(n, row[0]))
print("")
</code></pre>
<p>不过,我也不确定重复的数字是否有意义。在</p>
<p>要根据匹配的数量对行进行分组,可以使用dict将sum用作键并附加第一列值:</p>
<pre><code>from itertools import islice, imap
import csv
from collections import defaultdict
with open("in.txt") as f,open("numbers.txt") as nums:
# make a set of all winning nums
nums = set(imap(str.rstrip, nums))
r = csv.reader(f)
results = defaultdict(list)
# iterate over each row and sum how many matches we get
for row in r:
results[sum(n in nums for n in islice(row, 1, None))].append(row[0])
</code></pre>
<p>结果:</p>
<pre><code>defaultdict(<type 'list'>,
{0: ['a', 'e', 'g'], 1: ['b', 'd', 'h', 'i'],
2: ['c', 'f', 'j']})
</code></pre>
<p>键是数字匹配,值是与n个数字匹配的行id。在</p>