小编典典

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

python

我想做类似的事情:

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

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

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


阅读 199

收藏
2020-12-20

共1个答案

小编典典

好吧,你可以这样做:

>>> if all (k in foo for k in ("foo","bar")):
...     print "They're there!"
...
They're there!
2020-12-20