Saya mencoba menulis algoritma interpolasi tanggal untuk kamus:

  • setiap entri kamus memiliki N kunci
  • Setiap kunci untuk entri memiliki daftar tanggal yang sesuai
  • Tidak ada tanggal yang dapat diduplikasi di seluruh daftar kunci
  • Jika saya meratakan semua daftar untuk entri setelah diproses, saya akan berakhir dengan daftar tanggal harian yang berkelanjutan
  • interpolasi hanya boleh di antara tanggal yang tidak muncul dalam daftar lain (lihat contoh sederhana di bawah ini

Mantan. D = {"Masuk":{"list1":[1,2,9,12],"list2":[4,6]}}

Dfinal = {"Masuk":{"list1":[1,2,3,7,8,9,10,11,12],"list2":[4,5,6]}}

Dalam kode sampel saya, saya menggabungkan semua daftar menjadi satu, mengurutkan daftar itu dan kemudian mendapatkan indeks dari masing-masing tanggal yang kemudian dapat saya interpolasi di antara jika indeks luarnya sama yaitu. [0][1] [0][2] masalah yang saya alami adalah mencari cara untuk beralih dan interpolasi pada daftar lain ketika saya mencapai tanggal yang ada di daftar lain. Saya hampir sampai, saya hanya tidak yakin bagaimana menyelesaikannya, ini cukup membingungkan!

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
Tyler Cowan 15 November 2017, 00:56

1 menjawab

Jawaban Terbaik

TERSELESAIKAN

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))
0
Tyler Cowan 15 November 2017, 01:05