python:如何检查一个dict中的多个键? [英] python: How do I check that multiple keys are in a dict in one go?

查看:318
本文介绍了python:如何检查一个dict中的多个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些像

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

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

我不知道是否可能,但想知道。 : - )

I'm not sure if its possible but would like to know. :-)

推荐答案

嗯,你可以这样做:

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

这篇关于python:如何检查一个dict中的多个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆