【问题标题】:Interpolating dates across N python Lists在 N 个 python 列表中插入日期
【发布时间】:2017-11-14 21:56:22
【问题描述】:

我正在尝试为字典编写日期插值算法:

  • 每个字典条目都有 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])

【问题讨论】:

    标签: python list date dictionary interpolation


    【解决方案1】:

    已解决

    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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-29
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多