简体   繁体   中英

Popping out tuples in a list of tuples that match neighbor list of tuples

One list of tuples reads:

[(5,), (4,), (7,)]

The second one reads:

[(7,'James',6,1), (3,'Don',4,3), (2,'Poppy',5,1), (4,'Dom',6,4)]

I wish to pop out tuples in the second list if the first element is to be found on the first list. Ie, returning the second list as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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