我有一个for循环,如下所示:
public static Configuration CreateFromDictionary(Dictionary<string, string> dict)
{
var mappings = new Dictionary<string, Action<Configuration, string>>
{
[nameof(Name)] = (x, value) => x.Name = value,
[nameof(Url)] = (x, value) => x.Url = value,
[nameof(Password)] = (x, value) => x.Password = value,
};
var missingKeys = mappings.Keys
.Except(dict.Keys)
.ToArray();
if (missingKeys.Any())
throw new KeyNotFoundException("The given keys are missing: " + string.Join(", ", missingKeys));
return mappings.Aggregate(new Configuration(), (config, mapping) =>
{
mapping.Value(config, dict[mapping.Key]);
return config;
});
}
这个数字每天都在增加。我希望这一步是一个星期。 可以跳过6天,但这真是太糟糕了。 该怎么做?
for mydate in daterange (startDate, endDate):
答案 0 :(得分:2)
只需将步长添加到您的循环中即可:
from datetime import timedelta, date
def daterange(start_date, end_date):
for n in range(0, int((end_date - start_date).days) + 1, 7):
yield start_date + timedelta(n)
start_dt = date(2018, 9, 1)
end_dt = date(2018, 10, 30)
for dt in daterange(start_dt, end_dt):
print(dt.strftime("%Y-%m-%d"))
输出:
2018-09-01
2018-09-08
2018-09-15
2018-09-22
2018-09-29
2018-10-06
2018-10-13
2018-10-20
2018-10-27
答案 1 :(得分:1)
您使用范围为7的范围对象,并将其添加到开始日期:
import datetime
def daterange(startdate, enddate, steps=1):
"""Yields dates in increments of steps days from startdate to enddate"""
numdays = (enddate-startdate).days
for x in range (0, numdays, steps):
yield startdate + datetime.timedelta(days = x)
today = datetime.datetime.today()
end = today + datetime.timedelta(days=40)
print( list(daterange(today, end, 7)))
输出:
[datetime.datetime(2018, 9, 28, 19, 34, 26, 976681),
datetime.datetime(2018, 10, 5, 19, 34, 26, 976681),
datetime.datetime(2018, 10, 12, 19, 34, 26, 976681),
datetime.datetime(2018, 10, 19, 19, 34, 26, 976681),
datetime.datetime(2018, 10, 26, 19, 34, 26, 976681),
datetime.datetime(2018, 11, 2, 19, 34, 26, 976681)]