Python - 缩短if / for循环

时间:2011-11-07 19:07:42

标签: python coding-style if-statement for-loop iterator

我有几行代码用于迭代列表中的dict,我希望缩短它。它的工作原理很完美,但看起来代码太多了,我试图了解如何在Python中保持代码有效(或者一般来说)。

for d in dev['devices']:
    if d['name'] == devName:
        devFound = True
        break

'dev'的结构有点令人困惑,但对于我关心的数据: dev(dict)>设备(列表)> 0-n(字典)

值'name'是内编号字典中的一个键(根据其他地方的搜索值而变化),需要根据用户输入(devName)进行检查

任何输入都非常感激

3 个答案:

答案 0 :(得分:7)

基本相同,只用一些内置函数和生成器重写:

devFound = any(d['name'] == devName for d in dev['devices'])

答案 1 :(得分:4)

您可以尝试使用any()功能:

any(d for d in dev["devices"] if d['name'] == devName)

答案 2 :(得分:1)

以下是CédricJulien答案的变体,因为它可能会在某些(罕见)情况下失败:

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,因此在d中测试any()的真值是没有意义的。但是,使用True可以正常工作。

PS :时间测试表明any(True … for … if … == …)方法比双重的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_VALUEPOP_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