我有兩個字典,其中兩個字典的值都是日期串列。我想做的是匹配兩個字典中的鍵,然后比較它們之間的日期串列以確定日期之間的總天數。但是,我只想在第一個字典中的日期在第二個字典中的日期之后的條件下執行此操作。
例如,如果我有字典 1dict1和字典 2 dict,如下所示:
dict1 = {... , a : ['2022-07-27'] , ...}
dict2 = {... , a : ['2022-07-21'] , ...}
然后首先,兩者中的鍵都a匹配,然后從日期串列中減去日期和日期,得到 6 天。這個例子對我來說很容易撰寫代碼,但是我遇到的問題是日期串列變得更長更復雜。例如:dict1dict2dict1dict2
dict1 = {... , b : ['2021-09-14', '2022-08-08'], ...}
dict2 = {... , b : ['2022-08-01'], ...}
現在,由于第一個日期在dict1日期之前dict2,我不想減去它。但是,第二個日期在dict1日期之后dict2,所以我想減去它來確定中間的天數,即(2022-08-08) - (2022-08-01) = 7 days
另一個例子如下:
dict1 = {... , c : ['2021-07-28', '2022-07-07', '2022-09-17'], ...}
dict2 = {... , c : ['2022-05-01', '2022-07-27'], ...}
和前面的例子一樣,因為第一個日期在dict1日期之前dict2,我不想減去它。但是,由于 in 中的第二個日期在dict1中的第一個日期之后dict2,我確實想減去它以確定其之間的天數,即(2022-07-07) - (2022-05-01) = x number of days. 而且,由于第三個日期在dict1第二個日期之后dict2,我還想減去它以確定中間的天數(2022-09-17) - (2022-07-27) = y number of days,因為我現在有兩個值x和y,我想把它們加在一起得到總數,即x y = total number of days
有沒有一種計算簡便的方法來做到這一點?謝謝!
uj5u.com熱心網友回復:
這是一種你可以做你想做的事的方式:
這個答案假設所有日期都按時間順序排列
import datetime
def convert_to_time_obj(str_date):
return datetime.datetime.strptime(str_date, "%Y-%m-%d")
# get common keys between both dicts
dict1_keys = set(dict1.keys())
dict2_keys = set(dict2.keys())
common_keys = dict1_keys.intersection(dict2_keys)
# compare dict elements
for key in common_keys: # go through all common keys
total_days = 0 # used as a sum for case #3
multiple_dates_used = False # triggers the printing of total_days
last_index = 0 # assuming that all of the dates are in order from earliest to latest. to save time don't go through previously looked at values
for start_value in dict2[key]:
start_date = convert_to_time_obj(start_value)
for index, compare_value in enumerate(dict1[key][last_index:]): # grab the index to selectively not go through those element again based on the dates in dict2 having to be larger
compare_date = convert_to_time_obj(compare_value)
if compare_date <= start_date:
continue # skip over these dates
days_between = compare_date - start_date
print(days_between) # prints the days between only 2 dates
multiple_dates_used = bool(total_days) # updates to True when total_days > 0, therefore this would be a total of 2 or more dates with in between days
total_days = days_between.days
last_index = index 1
break # as only using the first set of dates for in between days
if multiple_dates_used:
print(total_days) # prints all of the days as in case 3
讓我知道這是否有幫助以及您所描述的內容我可能遺漏的任何內容。
uj5u.com熱心網友回復:
這是一個替代方案(盡管與 Andrew 的解決方案沒有太大區別),它使用堆疊而不是索引簿記。我覺得這樣更易讀。(這里的假設也是日期串列按升序排列)。
from datetime import datetime as dt
dict1 = ...
dict2 = ...
def to_date_stack(strings):
return [dt.strptime(string, "%Y-%m-%d") for string in reversed(strings)]
res = {}
for key in dict1.keys() & dict2.keys():
res[key] = 0
stack1, stack2 = map(to_date_stack, (dict1[key], dict2[key]))
while stack2 and stack1:
date2 = stack2.pop()
while stack1:
if (date1 := stack1.pop()) > date2:
res[key] = (date1 - date2).days
break
print(res)
該to_date_stack函式以相反的順序將串列項轉換為日期時間。一旦完成,處理可以簡單地通過從堆疊中彈出專案來完成。我做了一些測驗,發現性能沒有差異。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/521362.html
