我有一本这样的字典:
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/