简体   繁体   English

在 python 中,如何创建列表列表,其中每个列表都包含开始和结束日期的字符串表示形式?

[英]In python, how do I create a list of lists where each list contains a string representation of a start and end date?

My desired output would be this:我想要的 output 是这样的:

date_list = [
                ['2021-04-15', '2021-04-20'],
                ['2021-04-21', '2021-04-26'],
                ['2021-04-27', '2021-05-02'],
                ['2021-05-03', '2021-05-08'],
                ['2021-05-09', '2021-05-16'],
        ]

I would like to be able to define a start_date ('2021-04-15'), an end_date ('2021-05-16'), and an integer representing how many days I would like in each chunk (for example, I want each sub-list to contain a 5-day chunk).我希望能够定义一个 start_date ('2021-04-15')、一个 end_date ('2021-05-16') 和一个 integer 代表我希望在每个块中的天数(例如,我希望每个子列表包含一个 5 天的块)。 I also need to make sure that the end_date is included in the last list/chunk regardless of the size of that chunk.我还需要确保 end_date 包含在最后一个列表/块中,无论该块的大小如何。 I need to be able to use the elements of each list to parameterize a SQL loop and have attempted many solutions and none have been even close to what I am looking for.我需要能够使用每个列表的元素来参数化 SQL 循环并尝试了许多解决方案,但没有一个与我正在寻找的解决方案相近。

>>> import datetime
>>> start = datetime.datetime(year=2021, month=4, day=15)
>>> end = datetime.datetime(year=2021, month=5, day=16)
>>> delta = datetime.timedelta(days=5)
>>> result = []
>>> while start <= end:
        result.append([start.strftime('%Y-%m-%d'), (start+delta).strftime('%Y-%m-%d')])
        start += delta + datetime.timedelta(days=1)
>>> result
[['2021-04-15', '2021-04-20'],
 ['2021-04-21', '2021-04-26'],
 ['2021-04-27', '2021-05-02'],
 ['2021-05-03', '2021-05-08'],
 ['2021-05-09', '2021-05-14'],
 ['2021-05-15', '2021-05-20']]

This will work:这将起作用:

import datetime
import pandas as pd

def get_date_chunks(start_date, end_date, chunk_size):
    curr = datetime.datetime.strptime(start_date, "%Y-%m-%d")
    end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d")
    offset = pd.DateOffset(days=chunk_size)
    result = []
    while curr <= end_date:
        result.append([str(curr.date()), str((curr + offset).date())])
        curr += pd.DateOffset(days = chunk_size + 1)
        
    # Make sure last date matches up with the end_date
    if datetime.datetime.strptime(result[-1][0], "%Y-%m-%d") > end_date:
        result.pop()
    else:
        result[-1][1] = str(end_date.date())
    return result 
    
get_date_chunks('2021-04-15', '2021-05-16', 5)

This line:这一行:

if datetime.datetime.strptime(result[-1][0], "%Y-%m-%d") > end_date:

Means that your final list could be ['2021-05-15', '2021-05-15'] , which guarentees that you will always include the end date, but you will never see a day beyond your end date.意味着您的最终列表可能是['2021-05-15', '2021-05-15'] ,这保证您将始终包含结束日期,但您永远不会看到结束日期之后的一天。

This one does it exactly as you specified in your desired output.这个完全按照您在所需的 output 中指定的方式进行。 If the last chunk would be smaller than 5, it simply unites it with the previous one.如果最后一个块小于 5,它只是将它与前一个块合并。

import datetime
from dateutil import parser

def create_date_list(start_date, end_date, chunk_size):
    start_date = parser.parse(start_date)
    end_date = parser.parse(end_date)
    
    dates = []
    
    i = 0
    while True:
        chunk_start = start_date + i*datetime.timedelta(chunk_size) + datetime.timedelta(i)
        chunk_end = chunk_start + datetime.timedelta(chunk_size)
        if (end_date - chunk_end).days <= chunk_size:
            dates.append([str(chunk_start).split()[0], str(end_date).split()[0]])
            break
        else:
            dates.append([str(chunk_start).split()[0], str(chunk_end).split()[0]])
        i += 1
    
    return dates

暂无
暂无

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

相关问题 (python-pptx)如何创建幻灯片,其中每张幻灯片的标题都是列表中的字符串? - (python-pptx) How do I create slides where the title of each slide is a string from a list? 将列表列表的字符串表示形式转换为 python 中的列表 - convert a string representation of a list of lists to a list in python 将列表列表的字符串表示形式转换为列表 python 的列表,无需 eval - Convert string representation of list of lists to list of list python without eval 如何从列表末尾移动到Python列表的开头? - How to move from end of list, to start in Python's lists? 如何在 Python 的列表中创建列表? 可能吗? - How do I create lists in a list in Python? Is it possible? 如何在 python 中创建要串的列表列表? - how to Create list of lists to string in python? 我可以将字符串拆分为嵌套列表,其中外部列表​​包含每个句子的列表,内部列表包含每个句子的单词吗? - Can I split a string into a nested list where the outer list contains a list for each sentence and the inner list contains the words of each sentence? 如何快速转换为列表列表,在每个元素的开头插入一个字符串? - How can I quickly convert to a list of lists, insert a string at the start of each element? 如何在列表列表中的每个列表中添加元素? - How do I add an element to each list in a list of lists? 如何在列表列表中选择每个列表的前几个元素? - How do I select the first elements of each list in a list of lists?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM