Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
387 views
Welcome To Ask or Share your Answers For Others

1 Answer

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...