python 获取python for循环中的第一项和最后一项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/989665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the first and last item in a python for loop
提问by T. Stone
Is there an elegant and pythonic way to trap the first and last item in a for loop which is iterating over a generator?
是否有一种优雅且 Pythonic 的方式来捕获迭代生成器的 for 循环中的第一项和最后一项?
from calendar import Calendar
cal = Calendar(6)
month_dates = cal.itermonthdates(year, month)
for date in month_dates:
if (is first item): # this is fake
month_start = date
if (is last item): # so is this
month_end = date
This code is attempting to get the first day of the week the month ends on, and the last day of the week the month ends on. Example: for June, month-start should evaluate to 5/31/09. Even though it's technically a day in May, it's the first day of the week that June begins on.
此代码试图获取该月结束的一周的第一天,以及该月结束的一周的最后一天。示例:对于六月,month-start 应评估为 5/31/09。尽管技术上是五月的一天,但它是六月开始的一周的第一天。
Month-dates is a generator so i can't do the [:-1] thing. What's a better way to handle this?
Month-dates 是一个生成器,所以我不能做 [:-1] 的事情。有什么更好的方法来处理这个问题?
回答by Matthew Flaschen
I would just force it into a list at the beginning:
我会在开始时将它强制放入一个列表中:
from calendar import Calendar, SUNDAY
cal = Calendar(SUNDAY)
month_dates = list(cal.itermonthdates(year, month))
month_start = month_dates[0]
month_end = month_dates[-1]
Since there can only be 42 days (counting leading and tailing context), this has negligible performance impact.
由于只能有 42 天(计算领先和落后的上下文),这对性能的影响可以忽略不计。
Also, using SUNDAY is better than a magic number.
此外,使用 SUNDAY 比使用幻数更好。
回答by Tom Anderson
Richie's got the right idea. Simpler:
里奇的想法是正确的。更简单:
month_dates = cal.itermonthdates(year, month)
month_start = month_dates.next()
for month_end in month_dates: pass # bletcherous
回答by RichieHindle
How about this?
这个怎么样?
for i, date in enumerate(month_dates):
if i == 0:
month_start = date
month_end = date
enumerate()
lets you find the first one, and the date
variable falls out of the loop to give you the last one.
enumerate()
让您找到第一个,date
变量退出循环以提供最后一个。
回答by John Y
For this specific problem, I think I would go with Matthew Flaschen's solution. It seems the most straightforward to me.
对于这个特定问题,我想我会采用 Matthew Flaschen 的解决方案。这对我来说似乎是最直接的。
If your question is meant to be taken more generally, though, for any generator (with an unknown and possibly large number of elements), then something more like RichieHindle's approach may be better. My slight modification on his solution is not to enumerate and test for element 0, but rather just grab the first element explicitly:
但是,如果您的问题是针对任何生成器(具有未知且可能包含大量元素)的更普遍的问题,那么更像 RichieHindle 的方法可能会更好。我对他的解决方案的轻微修改不是枚举和测试元素 0,而是明确地获取第一个元素:
month_dates = cal.itermonthdates(year, month)
month_start = month_dates.next()
for date in month_dates:
pass
month_end = date