gpt4 book ai didi

python - 从元组列表中获取匹配值

转载 作者:行者123 更新时间:2023-11-28 22:17:21 25 4
gpt4 key购买 nike

我有一个包含元组的列表。我正在尝试为元组中的匹配项查找值。例如,如果 Ma 中,那么它应该返回 10。有人可以帮我弄清楚我在这里缺少什么吗?

>>> c
'M'
>>> a
[[('N', '64')], [('W', '1024')], [('M', '10')], [('C', '2')], [('RA', '8')]]

>>> c in a
False

>>> a[2][0][0]
'M'

>>> [item for item in a if a[item][0][0] == c]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not list

最佳答案

你可以把你的列表转换成字典

d = dict(map(lambda x: x[0], a))   

并使用简单的查找:

>>> d['M']
10

关于python - 从元组列表中获取匹配值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51485722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com