如何在字典中获取其他值,然后一个值等于

问题描述 投票:1回答:2

我有一个字典列表。当另一个值是'x']时,我想获取字典对的值

list_dict = [
              {"name": "abc", "value": "first_three"}, 
              {"name": "mno", "value": "middle"}, 
              {"name": "xyz", "value": "last_three"}
            ]

first_three时如何获得abc

这是我正在做的,但没有得到任何结果。

res = [ sub['name'] for sub in d['alphabets'] ]
            if res == 'abc':
               val = [ sub['value'] for sub in d['alphabets'] ]
               print(val)

我没有得到任何结果。 [first_three

如何获取name == 'abc

我有一个字典列表。当另一个值是'x'时,我想获取字典对的值list_dict = [{“ name”:“ abc”,“ value”:“ first_three”},{“ ...

python python-3.x list dictionary for-loop
2个回答
0
投票
使用列表理解:

0
投票
for d in list_dict: if d['name'] == 'abc': print d['value']
© www.soinside.com 2019 - 2024. All rights reserved.