gpt4 book ai didi

python - 更改字典中的多个键,同时在 Python 中执行 timedelta 操作

转载 作者:太空狗 更新时间:2023-10-30 01:33:39 25 4
gpt4 key购买 nike

我有一本字典,其中的键是整数。我随意将其中一个键更改为日期,我需要更改其他键。

示例数据:

{'C-STD-B&M-SUM': {datetime.date(2015, 7, 12): 0,
-1: 0.21484699999999998,
-2: 0.245074,
-3: 0.27874}

预期输出:

{'C-STD-B&M-SUM': {datetime.date(2015, 7, 12): 0,
datetime.date(2015, 7, 11): 0.21484699999999998,
datetime.date(2015, 7, 10): 0.245074,
datetime.date(2015, 7, 9): 0.27874}

目前的代码:

def change_start_date(dictionary_with_temporal_distribution):
unsw_mid_year_end_date = datetime.date(2015, 7, 12)
dictionary_with_temporal_distribution['C-STD-B&M-SUM'][unsw_mid_year_end_date] = dictionary_with_temporal_distribution['C-STD-B&M-SUM'][0]
del dictionary_with_temporal_distribution['C-STD-B&M-SUM'][0]
for k, v in dictionary_with_temporal_distribution['C-STD-B&M-SUM'].items():

最佳答案

你可以尝试类似的东西-

def change_start_date(dictionary_with_temporal_distribution):
unsw_mid_year_end_date = datetime.date(2015, 7, 12)
for k in list(dictionary_with_temporal_distribution['C-STD-B&M-SUM'].keys()):
dictionary_with_temporal_distribution['C-STD-B&M-SUM'][unsw_mid_year_end_date + timedelta(days=k)] = dictionary_with_temporal_distribution['C-STD-B&M-SUM'][k]
del dictionary_with_temporal_distribution['C-STD-B&M-SUM'][k]

关于python - 更改字典中的多个键,同时在 Python 中执行 timedelta 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30748670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com