如何检查字典,其中值是该列表元素的列表?

car*_*rmo 0 python dictionary list

如果我有一个字典,其中每个值都是一个列表,我如何检查列表中是否有特定的元素?例如:

myDict = { 0 : ['a','b','c'],
           1 : ['d','e','f']}
Run Code Online (Sandbox Code Playgroud)

我如何检查是否'a'存在?

sch*_*ggl 8

你可以使用any:

any('a' in lst for lst in myDict.values())
Run Code Online (Sandbox Code Playgroud)

这将停止迭代并评估True第一个查找.any是以下模式的内置快捷方式:

for x in y:
    if condition:
        return True
return False
# return any(condition for x in y)
Run Code Online (Sandbox Code Playgroud)