Python列表检索数据列表 [英] Python list of list retrieve data

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

问题描述

具有这样的列表列表:

data = [['a','x'], ['b','q'], ['c','z']]
search = 'c'
any(e[0] == search for e in data)

这将返回布尔值,但是如果我想检索出现的第一对其他seach变量(换句话说,当我搜索'a'时我想检索'x')怎么办?

This returns boolean value but what if I want to retrieve the first appearing other pair of seach variable (in other words I want to retrieve 'x' when I search 'a' )?

推荐答案

您可以使用dict(data)['c']获取该对中的第二个值.

You can use dict(data)['c'] to obtain the second value in the pair.

dict(data)根据您的配对创建字典.请注意,这将返回单个结果,并且不能保证返回第一个匹配项.但是,如果您执行许多搜索并且知道没有重复项,那么使用字典会更快.

dict(data) creates a dictionary from your pairs. Note that this will return a single result, and it's not guaranteed to return the first match. But if you perform many searches and you know that you don't have duplicates, it would be faster to use a dictionary.

否则,请使用zeekay的答案.

Otherwise, use zeekay's answer.

这篇关于Python列表检索数据列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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