[英]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.