Skip to main content
 首页 » 编程设计

python之将日期添加到嵌套列表中的列表项

2024年12月31日852php

我有一个列表列表作为输入,(下面的示例)- 我想要得到的是以下内容

1) 如果当前月份是上半年,则对于输入日期中的 i,将 current_year 添加到 i 的末尾。并且对于 i in year_second_half 将 yr + 1 添加到 i 中属于 year_second_half 的输入日期

示例 -“8 月 5 日 19:45”变为“2018 年 8 月 5 日 19:45”,“1 月 2 日 15:00”变为“2019 年 1 月 2 日 15:00”

2) 如果当前月份在 year_second_half 中,则对于 input_dates 中的 i,将当前年份添加到 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'] 

这似乎可行,但我想基本上以我收到的完全相同的格式和顺序输出列表列表,除了添加年份,如上所示。

当我运行我的代码时,我只收到一个列表。有一个更好的方法吗?此任务的目的只是用正确的年份更新列表。

请您参考如下方法:

所以这里发生的是 input_dates 的第一个 for 循环运行两次 次,因为您在一个列表 (input_dates) 中有 2 个列表.因此,您需要创建另一个列表,该列表将附加在执行每个 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)