我想查看列表中是否存在命名元组,类似于:
numbers = [1, 2, 3, 4, 5]
if 1 in numbers:
do_stuff()
有没有Pythonic(或没有)的方法来做到这一点?就像是:
namedtuples = [namedtuple_1, namedtuple_2, namedtuple3]
if (namedtuple with value x = 1) in namedtuples:
do stuff()
Use any http://docs.python.org/2/library/functions.html#any:
Demo:
>>> from collections import namedtuple
>>> A = namedtuple('A', 'x y')
>>> lis = [A(100, 200), A(10, 20), A(1, 2)]
>>> any(a.x==1 for a in lis)
True
>>> [getattr(a, 'x')==1 for a in lis]
[False, False, True]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)