我正在尝试创建一个嵌套字典来保存元组中的总数。外键将是产品id。嵌套字典将按月份和该月份发生的崩溃总数进行键控
示例:元组中的值是:(产品id、日期、崩溃数)
outages = [
('A','2018-01-01', 20),
('A','2018-01-01', 20),
('A','2018-01-01', 20),
('B','2018-01-15', 80),
('B','2018-01-19', 200),
('A','2018-02-08', 15),
('A','2018-02-09', 15),
('B','2018-02-15', 80),
('B','2018-02-15', 90),
('B','2018-02-20', 10),
('C','2018-02-25', 120),
('A','2018-03-01', 10),
('B','2018-04-01', 10),
('C','2018-03-01', 5)]
我的预期产出是:
{'A': {1: 60, 2: 30, 3: 10}, 'B': {1: 280, 2: 180, 4: 10}, 'C': {2: 120, 3: 5}}
到目前为止,我掌握的情况如下:
from datetime import datetime
#Create a class to implement missing method for when key is not in the dictionary
class NestedDict(dict):
def __missing__(self, key):
self[key] = NestedDict()
return self[key]
#Create Instance of NestedDict
nested_dic=NestedDict()
#Loop through outages and created outer and inner key
for x in outages:
nested_dic[x[0]][datetime.strptime(x[1], '%Y-%m-%d').month] = 0 #=>Need Help Here
我不知道从这里到哪里去得到想要的输出。我将这些值设置为0,因为执行:+= x[2]
会产生错误。理想情况下,我希望遍历outages
,并在扫描每个元组后更新字典,而不必多次迭代
如果您不介意使用^{} ,它可以非常简洁。其思想是提取月份,将其转换为
int
,并在每个嵌套字典上累加:如果您想要普通的
dict
(NestedDict
感觉像是一个不必要的抽象),可以从一开始就这样构建它们(手动设置默认键),或者使用如下方法:结果:
使用
collections
并扩展MutableMapping
,它是一个抽象类,具有python字典的接口。其思想是重写python dictionary
并定制__setitem__
方法:这是唯一需要深思熟虑地实现的方法结果
相关问题 更多 >
编程相关推荐