【发布时间】:2016-10-23 11:22:20
【问题描述】:
我有一个元组列表,我在一个简单的 for 循环中循环以识别包含某些条件的元组。
mytuplist =
[(1, 'ABC', 'Today is a great day'), (2, 'ABC', 'The sky is blue'),
(3, 'DEF', 'The sea is green'), (4, 'ABC', 'There are clouds in the sky')]
我希望它像这样高效且可读:
for tup in mytuplist:
if tup[1] =='ABC' and tup[2] in ('Today is','The sky'):
print tup
上面的代码不起作用,什么也没有打印出来。
下面的代码有效,但非常罗嗦。我如何使它像上面那样?
for tup in mytuplist:
if tup[1] =='ABC' and 'Today is' in tup[2] or 'The sky' in tup[2]:
print tup
【问题讨论】: