拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Python中每N次迭代的特定陈述句

Python中每N次迭代的特定陈述句

白鹭 - 2022-02-24 1946 0 0

我想创建一个回圈,每 12 个月,'year' 变量应该增加 1 直到达到限制。我一个人做不到。这是我尝试过的(在这种情况下,结局应该在 2013 年):

years=[]
begin= 2010
for i in range(0,40):
    year= begin
    if ((i % 12 == 0) and (i != 0)):       
        year =1
        years.append(year)
    else:
        years.append(year)
years

我得到的唯一结果是: [2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2011, 2010, 2010...]

非常感谢您的帮助!

uj5u.com热心网友回复:

这是你可以做的:

我假设该范围被认为是这里的月数。

years = [] # store the years from begin
begin = 2010
count_years = -1
for i in range(0,40,12):  # each 12 months a year - step 12
    count_years  =1  # add one year after each increment
    end  = begin   count_years  # find the end year until last increment
    years.append(end) # append to the list.

print(years)

打印年份将为您提供:

[2010, 2011, 2012, 2013]

我希望这是你需要的。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *