Common elements between two lists not using sets in Python
问题 I want count the same elements of two lists. Lists can have duplicate elements, so I can't convert this to sets and use & operator. a=[2,2,1,1] b=[1,1,3,3] set(a) & set(b) work a & b don't work It is possible to do it withoud set and dictonary? 回答1: In Python 3.x (and Python 2.7, when it's released), you can use collections.Counter for this: >>> from collections import Counter >>> list((Counter([2,2,1,1]) & Counter([1,3,3,1])).elements()) [1, 1] Here's an alternative using collections