You can do it like this:
from itertools import izip
def matches(input_lists, base_list):
for l in input_lists:
yield sum(1 for a, b in izip(l, base_list) if a==b)
and the result will be following:
>>> for i in matches([[1,2,3],[2,3,1],[0,2,0]], [1,2,4]):
i
2
0
1
which works as expected.
The izip()
function is generator function, which is better solution than zip()
. Also matches()
we defined is generator function, so there should be less problems when processing large lists.
Did it help? Is it clean enough?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…