【发布时间】:2015-02-01 08:02:10
【问题描述】:
我试图理解为什么以紧凑风格编写的代码有效,而以更冗长的风格编写的“相同的代码”不起作用。
此代码产生正确答案:
which_days = [5, 12, 19, 26]
buy_day = next((i for i in which_days if i in range(13, 20)), None)
print(buy_day)
>>> 19 <- correct answer
这段代码确实产生了一对括号:
which_days = [5, 12, 19, 26]
buy_day = []
for i in which_days:
if i in range((13, 20), None):
next(which_days)
print(buy_day)
>>>[] <- This is a pair of brackets
【问题讨论】:
标签: python-3.x list-comprehension next