我有一个清单列表作为输入(下面的示例)-而我想得出的是以下内容
1)如果当前月份在上半年,则对于输入日期中的i,将current_year添加到i的末尾。同样,对于year_second_half中的i,在落入year_second_half中的输入日期中,将yr +1加到i中
示例-'8月5日19:45'变为'2018年8月5日19:45'和'1月2日15:00'变为'2019年1月2日1日'
2)如果当前月份在year_second_half中,那么对于i在input_dates中,将当前年份添加到i的末尾,其中月份在下半年。而且在我所有的几个月里 属于上半年,将yr-1加到
示例(假设月份为5月)-“ 4月6日15:00”变为“ 2018年4月6日15:00”和“ 2017年8月5日19:45”
currentYear = datetime.now().year
this_yr = currentYear
currentMonth = datetime.now().month
this_month = currentMonth
year_first_half = ['August', 'September', 'October', 'November', 'December']
year_second_half = ['January', 'February', 'March', 'April', 'May']
input_dates = [['5th August 19:45','8th December 12:30','16th December 16:00',
'3rd January 20:00','12th January 15:00','19th January 15:00','30th January 20:00',
'2nd February 15:00'],['9th December 15:00','23rd December 15:00',
'27th December 20:00','2nd January 15:00','9th January 15:00',
'6th April 15:00','27th April 15:00','4th May 15:00','12th May 15:00']]
newlist = []
for x in input_dates:
for i in x:
for month in year_first_half:
if month in i and this_month in year_first_half:
i = (i + ' {}').format(this_yr)
elif month in i and this_month in year_second_half:
i = (i + ' {}').format(this_yr - 1)
for month in year_second_half:
if month in i and this_month not in year_second_half:
i = (i + ' {}').format(this_yr + 1)
elif month in i and this_month in year_second_half:
i = (i + ' {}').format(this_yr)
newlist.append(i)
print(newlist)
当前输出-
['5th August 19:45 2018', '8th December 12:30 2018', '16th December 16:00 2018', '3rd January 20:00 2019', '12th January 15:00 2019', '19th January 15:00 2019', '30th January 20:00 2019', '2nd February 15:00 2019', '9th December 15:00 2018', '23rd December 15:00 2018', '27th December 20:00 2018', '2nd January 15:00 2019', '9th January 15:00 2019', '6th April 15:00 2019', '27th April 15:00 2019', '4th May 15:00 2019', '12th May 15:00 2019']
这似乎可行,但我想以与接收时完全相同的格式和顺序输出列表清单,除了添加年份外,如上所示。
运行代码时,我只会收到一个列表。有一个更好的方法吗?此任务的目的只是用正确的年份更新列表。
答案 0 :(得分:1)
所以这里发生的是,for
的第一个input_dates
循环运行两次次,因为列表中有2个列表(input_dates)。因此,您需要创建另一个列表,该列表将追加在执行每个for循环时创建的列表。
我还修改了代码的逻辑。
这是修改后的代码:
from datetime import datetime
from dateutil.parser import parse
import numpy as np
import pandas as pd
currentYear = datetime.now().year
currentMonth = datetime.now().strftime("%B")
year_first_half = ['August', 'September', 'October', 'November', 'December']
year_second_half = ['January', 'February', 'March', 'April', 'May']
input_dates = [['5th August 19:45','8th December 12:30','16th December 16:00',
'3rd January 20:00','12th January 15:00','19th January 15:00','30th January 20:00',
'2nd February 15:00'],['9th December 15:00','23rd December 15:00',
'27th December 20:00','2nd January 15:00','9th January 15:00',
'6th April 15:00','27th April 15:00','4th May 15:00','12th May 15:00']]
outer_list = []
for x in input_dates:
inner_list = []
for i in x:
each_entry = parse(i)
if currentMonth in year_first_half:
if each_entry.strftime("%B") in year_first_half:
i = (i + ' {}').format(currentYear)
else:
i = (i + ' {}').format(currentYear + 1)
elif currentMonth in year_second_half:
if each_entry.strftime("%B") in year_second_half:
i = (i + ' {}').format(currentYear)
else:
i = (i + ' {}').format(currentYear - 1)
inner_list.append(i)
outer_list.append(inner_list)
print(outer_list)