简体   繁体   English

向嵌套列表中的列表项添加日期

[英]Adding dates to list items in nested lists

I have as input a list of lists, (sample below) - and what I am trying to get out is the following我有一个列表列表作为输入(下面的示例) - 我想要得到的是以下内容

1) If the current month is in year first half, then for i in input dates add the current_year to the end of i. 1) 如果当前月份在上半年,那么对于输入日期中的 i,将 current_year 添加到 i 的末尾。 And also for i in year_second_half add yr + 1 to i in input dates which fall in year_second_half并且对于 i in year_second_half 在属于 year_second_half 的输入日期中将 yr + 1 添加到 i

Example - '5th August 19:45' becomes '5th August 19:45 2018' and '2nd January 15:00' becomes '2nd January 15:00 2019'示例 -“8 月 5 日 19:45”变为“2018 年 8 月 5 日 19:45”,“1 月 2 日 15:00”变为“2019 年 1 月 2 日 15:00”

2) If the current month is in year_second_half, then for i in input_dates add the current year to the end of i where the month is in year second half. 2) 如果当前月份在 year_second_half 中,那么对于 input_dates 中的 i,将当前年份添加到 i 的末尾,其中月份在下半年。 And also for all months in i which fall in year first half, add yr - 1 to those并且对于 i 中属于上半年的所有月份,将 yr - 1 添加到那些

Example (presume month is May) - '6th April 15:00' becomes '6th April 15:00 2018' and '5th August 19:45 2017'示例(假设月份为 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)

Current output -电流输出 -

['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']

This seems to be working but I would like to output the list of lists basically in the exact same format and order that I received it except for adding the year, as shown above.这似乎有效,但我想以与我收到的完全相同的格式和顺序输出列表列表,除了添加年份,如上所示。

When I run my code I just receive one list.当我运行我的代码时,我只收到一个列表。 Is there a better way to do this?有一个更好的方法吗? The aim of this task is just to update the list with the correct years.此任务的目的只是用正确的年份更新列表。

So what happens here is that the 1st for loop of input_dates runs two times as you have 2 lists inside a list(input_dates).所以这里发生的事情是input_dates的第一个for循环运行两次,因为列表中有 2 个列表(input_dates)。 Hence you need to create another list which will append the list that was created in execution of each for loop.因此,您需要创建另一个列表,该列表将附加在执行每个 for 循环时创建的列表。

Also I have modified the logic of the code.我也修改了代码的逻辑。

Here is the modified code:这是修改后的代码:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM