我正在尝试为字典编写日期插值算法:
实施例。 D = {"条目":{" list1":[1,2,9,12]," list2":[4,6]}}
Dfinal = {" Entry":{" list1":[1,2,3,7,8,9,10,11,12]," list2& #34;:[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])
答案 0 :(得分:0)
<强>解决强>
nextafterf