将列表与元组列表进行比较? [英] Comparing a list to a a list of tuples?

查看:70
本文介绍了将列表与元组列表进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表.一个只是一个 id 列表

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

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

另一个是 id 和名称的元组列表

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

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

对这些进行设置比较的最简单方法是什么?我需要找出两件事 - 第一个列表中存在的 id 没有出现在第二个列表中,特别是 127, 316, 463 ,反之亦然,这将是 (222, "简").

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").

我使用的是 Python 2.5.

I am using Python 2.5.

推荐答案

首先,您至少应该升级到 Python 2.7,如果没有阻止您这样做的话.

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

如果要比较数字,可以将 idscombined 中每个列表的第一个元素进行比较:

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]

这篇关于将列表与元组列表进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆