在与元组的邻居列表匹配的元组列表中弹出元组

时间:2015-02-19 03:02:09

标签: python python-2.7

一个元组列表如下:

[(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)]

2 个答案:

答案 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)]