Finding intersection/difference between python lists

后端 未结 7 1581
别那么骄傲
别那么骄傲 2021-01-12 00:05

I have two python lists:

a = [(\'when\', 3), (\'why\', 4), (\'throw\', 9), (\'send\', 15), (\'you\', 1)]

b = [\'the\', \'when\', \'send\', \'we\', \'us\']
<         


        
7条回答
  •  南笙
    南笙 (楼主)
    2021-01-12 00:13

    A list comprehension will work.

    a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]
    b = ['the', 'when', 'send', 'we', 'us']
    filtered = [i for i in a if not i[0] in b]
    
    >>>print(filtered)
    [('why', 4), ('throw', 9), ('you', 1)]
    

提交回复
热议问题