繁体   English   中英

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

[英]Popping out tuples in a list of tuples that match neighbor list of tuples

一个元组列表显示为:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM