这是 Cédric Julien 答案的变体,因为它在某些(罕见)情况下可能会失败:
any(True for d in dev["devices"] if d['name'] == devName)
这是一个(虽然不寻常,但可能)案例,说明 any(True …) 给出正确结果,而 any(d …) 没有:
>>> class special_dict(dict):
... def __nonzero__(self):
... return False # All special_dict objects are False
...
>>> dev = {'devices': [special_dict(name="DEVNAME") for _ in xrange(10)]}
>>> any(d for d in dev["devices"] if d['name'] == "DEVNAME") # Incorrect
False
>>> any(True for d in dev["devices"] if d['name'] == "DEVNAME") # Correct
True
事实上,special_dict 对象的计算结果为 False,因此在any() 中测试d 的真值是没有意义的。不过,使用 True 可以。
PS:时序测试表明any(True … for … if … == …) 方法比doublep 的any(… == … for …) 解决方案更快:
python -m timeit -s "dev = {'devices': [{'name': 'BADNAME'} for _ in xrange(100)]}" "any(d['name'] == 'DEVNAME' for d in dev['devices'])"
100000 loops, best of 3: 16.3 usec per loop
python -m timeit -s "dev = {'devices': [{'name': 'BADNAME'} for _ in xrange(100)]}" "any(True for d in dev['devices'] if d['name'] == 'DEVNAME' )"
100000 loops, best of 3: 9.42 usec per loop
原因是第二个生成器最多返回一个值(True)。这可以通过反汇编两个生成器的 Python 代码来看出:
In [8]: def f(my_list):
...: return any(x == 11 for x in my_list)
In [12]: f.func_code.co_consts[1]
Out[12]: <code object <genexpr> at 0x1041f98b0, file "<ipython-input-8-384ce7986872>", line 2>
In [13]: dis.dis(_)
2 0 LOAD_FAST 0 (.0)
>> 3 FOR_ITER 17 (to 23)
6 STORE_FAST 1 (x)
9 LOAD_FAST 1 (x)
12 LOAD_CONST 0 (11)
15 COMPARE_OP 2 (==)
18 YIELD_VALUE
19 POP_TOP
20 JUMP_ABSOLUTE 3
>> 23 LOAD_CONST 1 (None)
26 RETURN_VALUE
此代码包含YIELD_VALUE 和POP_TOP,与此答案的版本相比,这需要更多时间:
In [14]: def g(my_list):
....: return any(True for x in my_list if x == 11)
In [15]: g.func_code.co_consts[1]
Out[15]: <code object <genexpr> at 0x1041f9630, file "<ipython-input-14-735c68947d80>", line 2>
In [16]: dis.dis(g.func_code.co_consts[1])
2 0 LOAD_FAST 0 (.0)
>> 3 FOR_ITER 23 (to 29)
6 STORE_FAST 1 (x)
9 LOAD_FAST 1 (x)
12 LOAD_CONST 0 (11)
15 COMPARE_OP 2 (==)
18 POP_JUMP_IF_FALSE 3
21 LOAD_GLOBAL 0 (True)
24 YIELD_VALUE
25 POP_TOP
26 JUMP_ABSOLUTE 3
>> 29 LOAD_CONST 1 (None)
32 RETURN_VALUE