一个元组列表如下:
[(5,), (4,), (7,)]
第二个是:
[(7,'James',6,1), (3,'Don',4,3), (2,'Poppy',5,1), (4,'Dom',6,4)]
如果要在第一个列表中找到第一个元素,我希望在第二个列表中弹出元组。即,返回第二个列表如下:
[(3,'Don',4,3), (2,'Poppy',5,1)]
答案 0 :(得分:2)
In [32]: list1 = [(5,), (4,), (7,)]
In [33]: list2 = [(7,'James',6,1), (3,'Don',4,3), (2,'Poppy',5,1), (4,'Dom',6,4)]
In [34]: [ x for x in list2 if x[:1] not in list1]
Out[34]: [(3, 'Don', 4, 3), (2, 'Poppy', 5, 1)]
答案 1 :(得分:1)
>>> x = [(5,), (4,), (7,)]
>>> y = [(7,'James',6,1), (3,'Don',4,3), (2,'Poppy',5,1), (4,'Dom',6,4)]
>>> set_x = set(x)
>>> y[:] = [t for t in y if t[:1] not in set_x]
>>> y
[(3, 'Don', 4, 3), (2, 'Poppy', 5, 1)]