在元组列表中查找元素 [英] Find an element in a list of tuples

查看:104
本文介绍了在元组列表中查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表"a"

a= [(1,2),(1,4),(3,5),(5,7)]

我需要找到一个特定数字的所有元组.说1就可以了

I need to find all the tuples for a particular number. say for 1 it will be

result = [(1,2),(1,4)]

我该怎么做?

推荐答案

如果只希望第一个数字匹配,则可以这样做:

If you just want the first number to match you can do it like this:

[item for item in a if item[0] == 1]

如果您只搜索其中包含1的元组:

If you are just searching for tuples with 1 in them:

[item for item in a if 1 in item]

这篇关于在元组列表中查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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