如何在 Python 中搜索元组列表 [英] How to search a list of tuples in Python

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

问题描述

所以我有一个像这样的元组列表:

So I have a list of tuples such as this:

[(1,"juca"),(22,"james"),(53,"xuxa"),(44,"delicia")]

我想要一个数值等于某值的元组的列表.

I want this list for a tuple whose number value is equal to something.

这样,如果我执行 search(53) 它将返回 2

So that if I do search(53) it will return the index value of 2

有没有简单的方法可以做到这一点?

Is there an easy way to do this?

推荐答案

[i for i, v in enumerate(L) if v[0] == 53]

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

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