Finding intersection/difference between python lists

后端 未结 7 1630
别那么骄傲
别那么骄傲 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条回答
  •  梦毁少年i
    2021-01-12 00:19

    Easy way

    a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]
    b = ['the', 'when', 'send', 'we', 'us']
    c=[] # a list to store the required tuples 
    #compare the first element of each tuple in with an element in b
    for i in a:
        if i[0] not in b:
            c.append(i)
    print(c)
    

提交回复
热议问题