小编典典

如何一次检查多个键是否在一个字典中?

all

我想做类似的事情:

foo = {
    'foo': 1,
    'zip': 2,
    'zam': 3,
    'bar': 4
}

if ("foo", "bar") in foo:
    #do stuff

如何检查foo和是否都bar在 dict 中foo


阅读 60

收藏
2022-05-06

共1个答案

小编典典

好吧,你可以这样做:

>>> if all (k in foo for k in ("foo","bar")):
...     print "They're there!"
...
They're there!
2022-05-06