在 N python 个列表中插入日期
Interpolating dates across N python Lists
我正在尝试为字典编写日期插值算法:
- 每个字典条目有 N 个键
- 条目的每个键都有对应的日期列表
- 键列表中的日期不能重复
- 如果我在处理后将一个条目的所有列表展平,我最终会得到一个连续的每日日期列表
- 插值只能在未出现在其他列表中的日期之间进行(参见下面的简化示例
例如。 D = {"Entry":{"list1":[1,2,9,12],"list2":[4,6]}}
Dfinal = {"Entry":{"list1":[1,2,3,7,8,9,10,11,12],"list2":[4, 5,6]}}
在我的示例代码中,我将所有列表合并为一个列表,对该列表进行排序,然后获取每个日期的索引,如果外部索引相同,则我可以在这些日期之间进行插值。 [0][1] [0][2] 我遇到的问题是弄清楚当我点击另一个列表中存在的日期时如何切换和插入另一个列表。我快到了,我只是不确定如何完成这个难题!
from datetime import date, timedelta as td
import datetime
import numpy as np
import calendar
#Interpolate Dates
def datesBetween(StartDate,EndDate):
dates = []
year1 = datetime.datetime.strptime(str(StartDate),'%d%b%Y').year
year2 = datetime.datetime.strptime(str(EndDate),'%d%b%Y').year
Month1 = datetime.datetime.strptime(str(StartDate),'%d%b%Y').month
Month2 = datetime.datetime.strptime(str(EndDate),'%d%b%Y').month
day1 = datetime.datetime.strptime(str(StartDate),'%d%b%Y').day
day2 = datetime.datetime.strptime(str(EndDate),'%d%b%Y').day
d1 = date(year1, Month1, day1)
d2 = date(year2, Month2, day2)
delta = d2 - d1
dates.append(datetime.datetime.strptime(str(StartDate),'%d%b%Y').strftime('%d%b%Y'))
d0 = datetime.datetime.strptime(str(StartDate),'%d%b%Y')
for i in range(delta.days):
d0 += datetime.timedelta(days=1)
dates.append(d0.strftime('%d%b%Y'))
return dates
#Sort Dates
def date_key(a):
a = datetime.datetime.strptime(a, '%d%b%Y').date()
return a
dates1 = ['28aug2017', '29aug2017', '30aug2017', '31aug2017', '01sep2017', '12sep2017','13sep2017', '14sep2017','20sep2017', '21sep2017', '25sep2017', '26sep2017','27sep2017', '28sep2017', '29sep2017', '02oct2017']
dates2 = ['05sep2017', '06sep2017', '07sep2017', '08sep2017', '11sep2017', '03oct2017']
dates3 = [ '15sep2017', '18sep2017', '19sep2017']
master = [dates1,dates2,dates3]
conc = []
for lst in master:
conc = conc + lst
sortedDates = sorted(conc,key=date_key)
indexMain = []
indexSub = []
for d in sortedDates:
indexMain.append(next((i for i, sublist in enumerate(master) if str(d) in sublist), -1))
indexSub.append(next((sublist for sublist in master if str(d) in sublist)).index(str(d)))
#interpolation of the respective lists
i = 0
while i < len(indexMain)-1:
d1 = master[indexMain[i]][indexSub[i]]
d2 = master[indexMain[i+1]][indexSub[i+1]]
if indexMain[i] == indexMain[i+1]:
temp = datesBetween(str(d1),str(d2))
for val in temp:
if val not in master[indexMain[i]]:
master[indexMain[i]].append(str(val))
if indexMain[i] != indexMain[i+1]:
temp = datesBetween(str(d1),str(d2))
for val in temp[:len(temp)-1]:
if val not in master[indexMain[i]]:
master[indexMain[i]].append(str(val))
i += 1
print(master[0])
print(master[1])
print(master[2])
已解决
from datetime import date, timedelta as td
import datetime
import numpy as np
import calendar
#Interpolate Dates
def datesBetween(StartDate,EndDate):
dates = []
year1 = datetime.datetime.strptime(str(StartDate),'%d%b%Y').year
year2 = datetime.datetime.strptime(str(EndDate),'%d%b%Y').year
Month1 = datetime.datetime.strptime(str(StartDate),'%d%b%Y').month
Month2 = datetime.datetime.strptime(str(EndDate),'%d%b%Y').month
day1 = datetime.datetime.strptime(str(StartDate),'%d%b%Y').day
day2 = datetime.datetime.strptime(str(EndDate),'%d%b%Y').day
d1 = date(year1, Month1, day1)
d2 = date(year2, Month2, day2)
delta = d2 - d1
dates.append(datetime.datetime.strptime(str(StartDate),'%d%b%Y').strftime('%d%b%Y'))
d0 = datetime.datetime.strptime(str(StartDate),'%d%b%Y')
for i in range(delta.days):
d0 += datetime.timedelta(days=1)
dates.append(d0.strftime('%d%b%Y'))
return dates
#Sort Dates
def date_key(a):
a = datetime.datetime.strptime(a, '%d%b%Y').date()
return a
dates1 = ['28aug2017', '31aug2017', '01sep2017']
dates2 = ['30aug2017']
dates3 = [ '03sep2017', '05sep2017']
master = [dates1,dates2,dates3]
conc = []
for lst in master:
conc = conc + lst
sortedDates = sorted(conc,key=date_key)
indexMain = []
indexSub = []
for d in sortedDates:
indexMain.append(next((i for i, sublist in enumerate(master) if str(d) in sublist), -1))
indexSub.append(next((sublist for sublist in master if str(d) in sublist)).index(str(d)))
i = 0
print(len(indexMain)-1)
while i < len(indexMain)-1:
d1 = master[indexMain[i]][indexSub[i]]
d2 = master[indexMain[i+1]][indexSub[i+1]]
if indexMain[i] == indexMain[i+1]:
temp = datesBetween(str(d1),str(d2))
for val in temp:
if str(val).lower() not in master[indexMain[i]]:
master[indexMain[i]].append(str(val).lower())
i += 1
else:
temp = datesBetween(str(d1),str(d2))
temp = temp[:len(temp)-1]
for val in temp:
if str(val).lower() not in master[indexMain[i]]:
master[indexMain[i]].append(str(val).lower())
i += 1
print(sorted(master[0],key=date_key))
print(sorted(master[1],key=date_key))
print(sorted(master[2],key=date_key))
我正在尝试为字典编写日期插值算法:
- 每个字典条目有 N 个键
- 条目的每个键都有对应的日期列表
- 键列表中的日期不能重复
- 如果我在处理后将一个条目的所有列表展平,我最终会得到一个连续的每日日期列表
- 插值只能在未出现在其他列表中的日期之间进行(参见下面的简化示例
例如。 D = {"Entry":{"list1":[1,2,9,12],"list2":[4,6]}}
Dfinal = {"Entry":{"list1":[1,2,3,7,8,9,10,11,12],"list2":[4, 5,6]}}
在我的示例代码中,我将所有列表合并为一个列表,对该列表进行排序,然后获取每个日期的索引,如果外部索引相同,则我可以在这些日期之间进行插值。 [0][1] [0][2] 我遇到的问题是弄清楚当我点击另一个列表中存在的日期时如何切换和插入另一个列表。我快到了,我只是不确定如何完成这个难题!
from datetime import date, timedelta as td
import datetime
import numpy as np
import calendar
#Interpolate Dates
def datesBetween(StartDate,EndDate):
dates = []
year1 = datetime.datetime.strptime(str(StartDate),'%d%b%Y').year
year2 = datetime.datetime.strptime(str(EndDate),'%d%b%Y').year
Month1 = datetime.datetime.strptime(str(StartDate),'%d%b%Y').month
Month2 = datetime.datetime.strptime(str(EndDate),'%d%b%Y').month
day1 = datetime.datetime.strptime(str(StartDate),'%d%b%Y').day
day2 = datetime.datetime.strptime(str(EndDate),'%d%b%Y').day
d1 = date(year1, Month1, day1)
d2 = date(year2, Month2, day2)
delta = d2 - d1
dates.append(datetime.datetime.strptime(str(StartDate),'%d%b%Y').strftime('%d%b%Y'))
d0 = datetime.datetime.strptime(str(StartDate),'%d%b%Y')
for i in range(delta.days):
d0 += datetime.timedelta(days=1)
dates.append(d0.strftime('%d%b%Y'))
return dates
#Sort Dates
def date_key(a):
a = datetime.datetime.strptime(a, '%d%b%Y').date()
return a
dates1 = ['28aug2017', '29aug2017', '30aug2017', '31aug2017', '01sep2017', '12sep2017','13sep2017', '14sep2017','20sep2017', '21sep2017', '25sep2017', '26sep2017','27sep2017', '28sep2017', '29sep2017', '02oct2017']
dates2 = ['05sep2017', '06sep2017', '07sep2017', '08sep2017', '11sep2017', '03oct2017']
dates3 = [ '15sep2017', '18sep2017', '19sep2017']
master = [dates1,dates2,dates3]
conc = []
for lst in master:
conc = conc + lst
sortedDates = sorted(conc,key=date_key)
indexMain = []
indexSub = []
for d in sortedDates:
indexMain.append(next((i for i, sublist in enumerate(master) if str(d) in sublist), -1))
indexSub.append(next((sublist for sublist in master if str(d) in sublist)).index(str(d)))
#interpolation of the respective lists
i = 0
while i < len(indexMain)-1:
d1 = master[indexMain[i]][indexSub[i]]
d2 = master[indexMain[i+1]][indexSub[i+1]]
if indexMain[i] == indexMain[i+1]:
temp = datesBetween(str(d1),str(d2))
for val in temp:
if val not in master[indexMain[i]]:
master[indexMain[i]].append(str(val))
if indexMain[i] != indexMain[i+1]:
temp = datesBetween(str(d1),str(d2))
for val in temp[:len(temp)-1]:
if val not in master[indexMain[i]]:
master[indexMain[i]].append(str(val))
i += 1
print(master[0])
print(master[1])
print(master[2])
已解决
from datetime import date, timedelta as td
import datetime
import numpy as np
import calendar
#Interpolate Dates
def datesBetween(StartDate,EndDate):
dates = []
year1 = datetime.datetime.strptime(str(StartDate),'%d%b%Y').year
year2 = datetime.datetime.strptime(str(EndDate),'%d%b%Y').year
Month1 = datetime.datetime.strptime(str(StartDate),'%d%b%Y').month
Month2 = datetime.datetime.strptime(str(EndDate),'%d%b%Y').month
day1 = datetime.datetime.strptime(str(StartDate),'%d%b%Y').day
day2 = datetime.datetime.strptime(str(EndDate),'%d%b%Y').day
d1 = date(year1, Month1, day1)
d2 = date(year2, Month2, day2)
delta = d2 - d1
dates.append(datetime.datetime.strptime(str(StartDate),'%d%b%Y').strftime('%d%b%Y'))
d0 = datetime.datetime.strptime(str(StartDate),'%d%b%Y')
for i in range(delta.days):
d0 += datetime.timedelta(days=1)
dates.append(d0.strftime('%d%b%Y'))
return dates
#Sort Dates
def date_key(a):
a = datetime.datetime.strptime(a, '%d%b%Y').date()
return a
dates1 = ['28aug2017', '31aug2017', '01sep2017']
dates2 = ['30aug2017']
dates3 = [ '03sep2017', '05sep2017']
master = [dates1,dates2,dates3]
conc = []
for lst in master:
conc = conc + lst
sortedDates = sorted(conc,key=date_key)
indexMain = []
indexSub = []
for d in sortedDates:
indexMain.append(next((i for i, sublist in enumerate(master) if str(d) in sublist), -1))
indexSub.append(next((sublist for sublist in master if str(d) in sublist)).index(str(d)))
i = 0
print(len(indexMain)-1)
while i < len(indexMain)-1:
d1 = master[indexMain[i]][indexSub[i]]
d2 = master[indexMain[i+1]][indexSub[i+1]]
if indexMain[i] == indexMain[i+1]:
temp = datesBetween(str(d1),str(d2))
for val in temp:
if str(val).lower() not in master[indexMain[i]]:
master[indexMain[i]].append(str(val).lower())
i += 1
else:
temp = datesBetween(str(d1),str(d2))
temp = temp[:len(temp)-1]
for val in temp:
if str(val).lower() not in master[indexMain[i]]:
master[indexMain[i]].append(str(val).lower())
i += 1
print(sorted(master[0],key=date_key))
print(sorted(master[1],key=date_key))
print(sorted(master[2],key=date_key))