简体   繁体   中英

Compare list of tuples with list and return list of tuple that not matched

I have list of tuples and a second list. I need to return a list of tuples for which the second item in the tuple does not appear in the second list.

listTuple =  [('IN', 'OS19014'), ('CA', 'OS0001'), ('GB', 'OS0002'), ('CA', 'OS0003')]
normalList = ['OS19014', 'OS0001', 'OS0002']

Expected_result:

[('CA', 'OS0003')]

Try this:

>>> [lt for lt in listTuple if not lt[1] in set(normalList)]
[('CA', 'OS0003')]

this code work for me:

listTuple = [('IN', 'OS19014'), ('CA', 'OS0001'), ('GB', 'OS0002'), ('CA', 'OS0003')] 
normalList = ['OS19014', 'OS0001', 'OS0002']
for i in listTuple:
    if i[1] not in normalList:
        print(i)

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