python - 如何检查字典中的列表是否是键?

标签 python list dictionary

我有一本这样的字典:

a = {'values': [{'silver': '10'}, {'gold': '50'}]}

现在我想检查一下字典中是否有“silver”键:
if 'silver' in a['values']:

但我收到错误:
NameError: name 'silver' is not defined

那么我怎样才能在 python 中实现呢?

最佳答案

您可以使用 any .

if any('silver' in d for d in a['values']):
   # do stuff

关于python - 如何检查字典中的列表是否是键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59697818/

相关文章:

python - 检查输入时出错 : expected embedding_1 input to have shape but got shape

python - 是否可以通过列表中的单词搜索 txt 文件并返回上面的行?

python - 使用字典、RobotFramework 时 Evaluate 和 SetVariable 之间的区别

python - 我可以将 c++ 模板类导出到 C,然后再导出到带有 ctypes 的 python 吗?

python - 如何设置一个带有一个参数和字母数字字符串的 url?

python - 将字符串列表转换为一个字符串

Python - list.remove(x) x 不在列表中

python - 如何根据值与列表过滤字典

python - 从递归嵌套字段列表创建部分字典

python - Django - 循环模板中的唯一模型字段