簡體   English   中英

如何檢查列表中的python字典值中是否存在元素?

[英]How to Check whether a element exists in a python dictionary value which is a list?

如何檢查dic1是否存在'111'

dic1 = {'images':[000,111,222,333,444],'name':['foo','bar','baz']}

我嘗試使用列表推導,但無法獲得布爾答案? 我得到了錯誤'int' object is not iterable

您可以遍歷dict.values並檢查是否存在:

dic1 = {'images':[000,111,222,333,444],'name':['foo','bar','baz']}
print(any(111 in b for b in dic1.values()))

輸出:

True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM