comparing lists python

后端 未结 1 589
没有蜡笔的小新
没有蜡笔的小新 2020-12-21 16:34

im trying to compare between one list and 3 other lists with the same number of indexes. for example I have this list

 [A, B, C]

and I have

1条回答
  •  醉梦人生
    2020-12-21 17:05

    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?

    0 讨论(0)
提交回复
热议问题