How do I match items in one list against items in another list in Python

前端 未结 2 448
故里飘歌
故里飘歌 2020-12-11 10:34

Having trouble finding a python solution to matching elements of one list against elements in another list without a whole pile of \"for\" and \"if\" loops. I\'m hoping to f

2条回答
  •  离开以前
    2020-12-11 11:22

    Trying to answer this part of the question matching elements of one list against elements in another list could use set(), for example:

    a = ['a','b','c','d','g']
    b = ['a','c','g','f','z']
    
    list(set(a).intersection(b)) # returns common elements in the two lists
    

提交回复
热议问题