【发布时间】:2014-02-19 05:21:12
【问题描述】:
def only_evens(lst):
"""
Return a list of the lists in lst that contain only even integers.
>>> only_evens([[1, 2, 4], [4, 0, 6], [22, 4, 3], [2]])
[[4, 0, 6], [2]]
"""
even_lists = []
condition = True
for sublist in lst:
for num in sublist:
if num % 2 != 0:
condition = condition and False
if condition == True:
even_lists.append(sublist)
return even_lists
我不明白为什么这总是返回一个空字符串?直觉上,这有意义吗?非常感谢您的帮助。我被困在这件事上太久了。
编辑:非常感谢大家!我现在明白了:)。
【问题讨论】:
标签: python list for-loop nested-forms nested-loops