我有一个字典列表。我想当另一个字典对的值为“x”时获取字典对的值
alphabets = [
{"name": "abc", "value": "first_three"},
{"name": "mno", "value": "middle"},
{"name": "xyz", "value": "last_three"}
]
当 abc
时,如何获取 first_third
。
这就是我正在做的事情,但我没有得到任何结果。
res = [ sub['name'] for sub in d['alphabets'] ]
if res == 'abc':
val = [ sub['value'] for sub in d['alphabets'] ]
print(val)
我没有得到任何结果。当 name == 'abc
first_third
最佳答案
使用列表理解:
res = [d['value'] for d in list_dict if d['name'] == 'abc']
>>> print(res)
['first_three']
关于python - 如何获取字典中的其他值然后一个值等于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59756499/