3 回答

TA贡献1816条经验 获得超6个赞
您可以使用any()
:
any(dict_obj.get(prop) is None for prop in properties_to_check_for )
如果property
在中找不到任何properties_to_check_for
值或值为,则将返回True None
。

TA贡献1909条经验 获得超7个赞
对于大型字典与大型列表比较,将set返回的-like对象viewkeys与的set版本进行比较properties_to_check_for可能会带来性能上的好处
if dict_obj.viewkeys() >= set(properties_to_check_for):
时序测量:
timeit.timeit('dict_obj.viewkeys() >= set(properties_to_check_for)',
setup='dict_obj = dict(zip(xrange(100000), xrange(100000))); properties_to_check_for=xrange(10000)',
number=10000)
9.82882809638977
timeit.timeit('all(key in dict_obj for key in properties_to_check_for)',
setup='dict_obj =dict(zip(xrange(100000),xrange(100000)));properties_to_check_for=list(xrange(10000))',
number=10000)
12.362821102142334
添加回答
举报