英文:
How to fill in missing dates in a list of tuples
问题
我有一些类似于以下的元组列表:
list_1 = [('2023-01-01', 'a'), ('2023-01-02', 'b'), ('2023-01-10', 'c')]
list_2 = [('2023-01-02', 'd'), ('2023-01-05', 'e'), ('2023-01-07', 'f')]
list_3 = [('2023-01-01', 'g'), ('2023-01-03', 'h'), ('2023-01-10', 'i')]
我需要为每个列表中的缺失日期填充None值:
list_1 = [('2023-01-01', 'a'), ('2023-01-02', 'b'), ('2023-01-03', None), ('2023-01-05', None), ('2023-01-07', None), ('2023-01-10', 'c')]
list_2 = [('2023-01-01', None), ('2023-01-02', 'd'), ('2023-01-03', None), ('2023-01-05', 'e'), ('2023-01-07', 'f'), ('2023-01-10', None)]
list_3 = [('2023-01-01', 'g'), ('2023-01-02', None), ('2023-01-03', 'h'), ('2023-01-05', None), ('2023-01-07', None), ('2023-01-10', 'i')]
元组元素的数量可以变化。
最佳和最有效的解决方案是什么?
英文:
I have couple lists of tuples like this ones:
list_1 = [('2023-01-01', 'a'), ('2023-01-02', 'b'), ('2023-01-10', 'c')]
list_2 = [('2023-01-02', 'd'), ('2023-01-05', 'e'), ('2023-01-07', 'f')]
list_3 = [('2023-01-01', 'g'), ('2023-01-03', 'h'), ('2023-01-10', 'i')]
I need to fill in the missing dates with None value for each of the lists:
list_1 = [('2023-01-01', 'a'), ('2023-01-02', 'b'), ('2023-01-03', None), ('2023-01-05', None), ('2023-01-07', None), ('2023-01-10', 'c')]
list_2 = [('2023-01-01', None), ('2023-01-02', 'd'), ('2023-01-03', None), ('2023-01-05', 'e'), ('2023-01-07', 'f'), ('2023-01-10', None)]
list_3 = [('2023-01-01', 'g'), ('2023-01-02', None), ('2023-01-03', 'h'), ('2023-01-05', None), ('2023-01-07', None)('2023-01-10', 'i')]
The number of tuple elements can vary.
What is the best and most efficient solution to do this ?
答案1
得分: 1
抱歉,代码部分不需要翻译。以下是您要翻译的内容:
"Unfortunately, python's builtin datetime
module doesn't have a 'get all dates in a range' function. But pandas
has one."
"Important note: all is done with strings, and it works because of the convenient format of the dates, so that lexicographical order of strings correspond to chronological order of dates."
英文:
I cannot prove that it's the "best and most efficient", but here is one approach.
Unfortunately, python's builtin datetime
module doesn't have a "get all dates in a range" function. But pandas
has one.
from pandas import date_range
list_1 = [('2023-01-01', 'a'), ('2023-01-02', 'b'), ('2023-01-10', 'c')]
dates_in_list1 = {d for d,_ in list_1}
dates_in_range = {str(d)[:10] for d in date_range(min(dates_in_list1), max(dates_in_list1), freq='d')}
missing_dates = dates_in_range.difference(dates_in_list1)
new_list_1 = sorted(list_1 + [(d, None) for d in missing_dates])
print(new_list_1)
# [('2023-01-01', 'a'), ('2023-01-02', 'b'), ('2023-01-03', None), ('2023-01-04', None), ('2023-01-05', None), ('2023-01-06', None), ('2023-01-07', None), ('2023-01-08', None), ('2023-01-09', None), ('2023-01-10', 'c')]
Important note: all is done with strings, and it works because of the convenient format of the dates, so that lexicographical order of strings correspond to chronological order of dates.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论