python - 如何获取字典中的其他值然后一个值等于

标签 python python-3.x list dictionary for-loop

我有一个字典列表。我想当另一个字典对的值为“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/

相关文章:

python - 带有传统除号 (÷) 的 eval() 问题

python - 安装python2.6构建依赖

python - 使用 PyQt4 和 python 3.5.2 运行 PyMoskito 示例时出错

python - 在Python中使用嵌套for循环加载字典

python - 当我不知道它的索引号时如何从列表中删除一个元素

Python:在列表第一个行中查找第一个列表的索引

python - 属性错误 : 'Twython' object has no attribute 'destroy_message'

python - Django 查看相同 url 模式请求的执行顺序?

python-3.x - GEKKO中优化问题的并行化

python - 如何在 Tensorflow 中以不同的学习率训练两个密集层?