简体   繁体   中英

Comparing a list to a a list of tuples?

I have two lists. One is simply a list of ids

ids = [123, 124, 127, 316, 463]

and the other is a list of tuples of id's and names

combined = [(123, "Brian"), (124,"Eric"), (222,"Jane")]

What is the easiest way to do set comparisons on these? I need to find out two things - ids that exist in the first list that don't appear in the second list, specifically 127, 316, 463 and vice versa, which would be (222, "Jane") .

I am using Python 2.5.

First of all, you should upgrade to Python 2.7 at the very least, if something doesn't prevent you from doing so.

You can compare ids to the first element of each list in combined if you want to compare the numbers:

ids = [123, 124, 127, 316, 463]
combined = [(123, "Brian"), (124,"Eric"), (222,"Jane")]
combined_first = [x[0] for x in combined]

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