[英]How to Check whether a element exists in a python dictionary value which is a list?
How to check whether '111'
exists in dic1
? 如何检查
dic1
是否存在'111'
?
dic1 = {'images':[000,111,222,333,444],'name':['foo','bar','baz']}
I tried using List comprehensions but unable to get the boolean answer? 我尝试使用列表推导,但无法获得布尔答案? I got the error
'int' object is not iterable
. 我得到了错误
'int' object is not iterable
。
You can iterate over dict.values
and check for existence: 您可以遍历
dict.values
并检查是否存在:
dic1 = {'images':[000,111,222,333,444],'name':['foo','bar','baz']}
print(any(111 in b for b in dic1.values()))
Output: 输出:
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.