【发布时间】:2019-02-21 15:19:43
【问题描述】:
假设您有一本字典,其中包含日期列表(从 excel 中提取为浮点数)作为县子字典中的值。
master_dictionary = {'MO': {
'Lincoln County': [43378.0, 43378.0, 43378.0],
'Franklin County': [43357.0, 43357.0],
'Camden County': [43208.0, 43208.0, 43208.0],
'Miller County': [43208.0],
'Morgan County': [43208.0, 43208.0]},
'WI': {'Marathon County': [43371.0, 43371.0, 43371.0, 43371.0, 43371.0]},
'NJ': {'Atlantic County': [43340.0, 43340.0]}}
我的目标是 1) 获取这些“日期”的最大值,2) 使用 datetime.strftime 将最大“日期”转换为 '%M/%D/%Y' 值。我能够获得最大值并将其转换,但我试图让它更新主字典中的日期值。我该怎么做?
for key, value in master_dictionary.items():
counties = value
for k, v in counties.items():
d = max(v)
year, month, day, hour, minute, second = xldate_as_tuple(d, book_datemode)
n_date = rawDate = (str(month) + "/" + str(day) + "/" + str(year))
print(n_date)
【问题讨论】:
-
你希望你的输出是什么样的?
-
类似这样的:
master_dictionary = {'MO': { 'Lincoln County': ['4/22/2018'], 'Franklin County': ['12/3/2017'], 'Camden County': ['7/11/2018'], 'Miller County': ['6/20/2018'],'Morgan County': ['6/6/2018']},'WI': {'Marathon County': ['12/21/2017']}, 'NJ': {'Atlantic County': ['10/5/2017']}}基本上,每个县都有最新的日期。如果我可以让日期不再是列表项,而只是“县”子词典中的常规值,则奖励。
标签: python list dictionary python-3.6 updates