Common elements between two lists not using sets in Python

后端 未结 3 1777
猫巷女王i
猫巷女王i 2020-12-06 06:26

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条回答
  •  时光说笑
    2020-12-06 06:54

    Using sets is the most efficient, but you could always do r = [i for i in l1 if i in l2].

提交回复
热议问题