Python 向嵌套列表中的列表项添加日期
我有一个列表列表作为输入,(下面的示例)-我想得到的是以下内容 1) 如果当前月份在上半年,则输入日期中的i将当前_年添加到i的末尾。对于年下半年的i,在年下半年的输入日期中向i添加yr+1 示例—“2018年8月5日19:45”变为“2018年8月5日19:45”,“2019年1月2日15:00”变为“2019年1月2日15:00” 2) 如果当前月份在下半年,则对于输入日期中的i,将当前年份添加到i末尾,该月份在下半年。在我生命中的所有月份 在今年上半年,加上yr-1 例如(假设月份为5月)-“4月6日15:00”变为“2018年4月6日15:00”和“2017年8月5日19:45”Python 向嵌套列表中的列表项添加日期,python,python-3.x,date,nested-lists,Python,Python 3.x,Date,Nested Lists,我有一个列表列表作为输入,(下面的示例)-我想得到的是以下内容 1) 如果当前月份在上半年,则输入日期中的i将当前_年添加到i的末尾。对于年下半年的i,在年下半年的输入日期中向i添加yr+1 示例—“2018年8月5日19:45”变为“2018年8月5日19:45”,“2019年1月2日15:00”变为“2019年1月2日15:00” 2) 如果当前月份在下半年,则对于输入日期中的i,将当前年份添加到i末尾,该月份在下半年。在我生命中的所有月份 在今年上半年,加上yr-1 例如(假设月份为5月)
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']
这似乎是工作,但我想输出的清单基本上在完全相同的格式和顺序,我收到它除了添加年份,如上所示
当我运行代码时,我只收到一个列表。有更好的方法吗?这项任务的目的只是用正确的年份更新列表 因此这里发生的是
input\u dates
的第一个循环运行两次,因为一个列表中有两个列表(input\u 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)
谢谢运行此命令,返回两个相同的列表。我希望返回的列表看起来像输入日期,但每个列表中每个列表项的末尾都有正确的年份。纳维看到我之前的评论了吗?有人对我需要什么有什么建议吗?@安菲尔德我修改了你的逻辑。纳维谢谢。这绝对是好的。唯一的问题是它用相同的元素打印列表两次