Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python在字典列表中生成一年的周期_Python - Fatal编程技术网

python在字典列表中生成一年的周期

python在字典列表中生成一年的周期,python,Python,我是python方面的新手。我试图在字典列表中生成一年的;像这样的, [{'end_date': '2016-01-31', 'start_date': '2016-01-01'}, {'end_date': '2016-01-31', 'start_date': '2016-01-01'}, {'end_date': '2016-01-31', 'start_date': '2016-01-01'}, {'end_date': '2016-01-31', 'start_date': '2

我是python方面的新手。我试图在字典列表中生成一年的;像这样的,

[{'end_date': '2016-01-31', 'start_date': '2016-01-01'},
 {'end_date': '2016-01-31', 'start_date': '2016-01-01'},
 {'end_date': '2016-01-31', 'start_date': '2016-01-01'},
 {'end_date': '2016-01-31', 'start_date': '2016-01-01'},
 {'end_date': '2016-01-31', 'start_date': '2016-01-01'},
 {'end_date': '2016-01-31', 'start_date': '2016-01-01'},
 {'end_date': '2016-01-31', 'start_date': '2016-01-01'},
 {'end_date': '2016-01-31', 'start_date': '2016-01-01'},
 {'end_date': '2016-01-31', 'start_date': '2016-01-01'},
 {'end_date': '2016-01-31', 'start_date': '2016-01-01'},
 {'end_date': '2016-01-31', 'start_date': '2016-01-01'},
 {'end_date': '2016-01-31', 'start_date': '2016-01-01'}]
对于此输出,我创建了以下函数:

def get_periods():
    year = raw_input('Enter an year')
    list_of_periods = []
    for k in range(12):
        list_of_periods.append({'start_date': year + '-01-01','end_date': year + '-01-31'})
    return list_of_periods
这里我只想通过
,它应该生成一个一年的周期,即

[{'end_date': '2016-01-31', 'start_date': '2016-01-01'},
 {'end_date': '2016-02-29', 'start_date': '2016-02-01'},
 ....... and so on]
但是我不知道如何迭代
来创建
的适当时段。还有闰年第30个月天或第31个月

您可以使用查找任何月份的最后一天:

import calendar

def get_periods():
    year = int(input('Enter a year: '))
    list_of_periods = []
    for month in range(1, 13):
        last_day = calendar.monthrange(year, month)[1]
        list_of_periods.append({'start_date': '{0}-{1:02}-01'.format(year, month),
                                'end_date': '{0}-{1:02}-{2}'.format(year, month, last_day)})

    return list_of_periods

print(get_periods())
运行此操作时,您将获得与以下类似的输出:

Enter a year: 2016
[{'start_date': '2016-01-01', 'end_date': '2016-01-31'}, 
 {'start_date': '2016-02-01', 'end_date': '2016-02-29'}, 
 {'start_date': '2016-03-01', 'end_date': '2016-03-31'}, 
 {'start_date': '2016-04-01', 'end_date': '2016-04-30'}, 
 ...]
您可以使用查找任意月份的最后一天:

import calendar

def get_periods():
    year = int(input('Enter a year: '))
    list_of_periods = []
    for month in range(1, 13):
        last_day = calendar.monthrange(year, month)[1]
        list_of_periods.append({'start_date': '{0}-{1:02}-01'.format(year, month),
                                'end_date': '{0}-{1:02}-{2}'.format(year, month, last_day)})

    return list_of_periods

print(get_periods())
运行此操作时,您将获得与以下类似的输出:

Enter a year: 2016
[{'start_date': '2016-01-01', 'end_date': '2016-01-31'}, 
 {'start_date': '2016-02-01', 'end_date': '2016-02-29'}, 
 {'start_date': '2016-03-01', 'end_date': '2016-03-31'}, 
 {'start_date': '2016-04-01', 'end_date': '2016-04-30'}, 
 ...]

正如你所说,这里有几个问题需要解决,不清楚哪个是这个问题的焦点。这使得这不是stackoverflow的一个想法问题,这就是为什么我投反对票。(不是因为我想让你泄气!)我建议你在这里回复一个清晰的问题,或者尝试一个更具互动性的网站,如dreamincode.net或daniweb。正如你所说,这里有几个问题需要解决,但不清楚哪个是这个问题的焦点。这使得这不是stackoverflow的一个想法问题,这就是为什么我投反对票。(不是因为我想让你泄气!)我建议你要么在这里回复一个明确的问题,要么尝试一个更具互动性的网站,比如dreamincode.net或daniweb。