如何从listinfo创建字典?

时间:2019-01-20 11:27:11

标签: python matplotlib data-visualization

我正在过滤与此模式有关的数据列表:

('timestamp', 'node_source', 'node_destination', 'node_source_counter_acces_to_specific_function')

我正在尝试提取和显示每个node_source的数据”。

import matplotlib.pyplot as plt

list_info= [('1547977394', '02141592cc0000000100000000000000', '02141592cc0000000500000000000000', '1'),
('1547977395', '02141592cc0000000100000000000000', '02141592cc0000000300000000000000', '2'), 
('1547977396', '02141592cc0000000100000000000000', '02141592cc0000000500000000000000', '3'), 
('1547977397', '02141592cc0000000100000000000000', '02141592cc0000000700000000000000', '4'), 
('1547977398', '02141592cc0000000100000000000000', '02141592cc0000000300000000000000', '5'), 
('1547977399', '02141592cc0000000100000000000000', '02141592cc0000000500000000000000', '6'), 
('1547977400', '02141592cc0000000500000000000000', '02141592cc0000000500000000000000', '7'),
('1547977401', '02141592cc0000000500000000000000', '02141592cc0000000500000000000000', '8'),
('1547977402', '02141592cc0000000200000000000000', '02141592cc0000000500000000000000', '9'),
('1547977403', '02141592cc0000000100000000000000', '02141592cc0000000500000000000000', '10'),
('1547977404', '02141592cc0000000200000000000000', '02141592cc0000000300000000000000', '11'),
('1547977405', '02141592cc0000000400000000000000', '02141592cc0000000500000000000000', '12'),
('1547977406', '02141592cc0000000300000000000000', '02141592cc0000000500000000000000', '13'),
('1547977407', '02141592cc0000000400000000000000', '02141592cc0000000500000000000000', '14'),
('1547977408', '02141592cc0000000400000000000000', '02141592cc0000000500000000000000', '15')]

#print(list_info)
for  i, item in enumerate(list_info):
    print(item[1])

dict_node = {}
for timestamp, id_src_node, id_dest_node, Counter in list_info:
        dict_node[id_src_node]["timestamp"].append(float(timestamp))
        dict_node[id_src_node]["Counter"].append(int(Counter))
        dict_node[id_dest_node]["dest"].append(id_dest_node)

print(dict_node)

但是,我遇到这个错误,我可以显示源节点的信息:

02141592cc0000000100000000000000
    dict_node[id_src_node]["timestamp"].append(float(timestamp))
02141592cc0000000100000000000000
02141592cc0000000100000000000000
02141592cc0000000100000000000000
02141592cc0000000100000000000000
02141592cc0000000100000000000000
02141592cc0000000500000000000000
KeyError: '02141592cc0000000100000000000000'

例如,必须显示给node_source:02141592cc0000000100000000000000的预期数据是:

('02141592cc0000000100000000000000',timestam':[1547977395,1547977396,1547977397,1547977398,1547977399],'Counter':[2,3,4,5,6],'dest':[02141592cc0000000300000000000000,02141592cc0000000500000000000000,02141592cc0000000700000000000000,02141592cc0000000300000000000000,02141592cc0000000500000000000000])

1 个答案:

答案 0 :(得分:0)

字典不存在时不会自动创建值。

您修改字典的第一行代码:

dict_node[id_src_node]["timestamp"].append(float(timestamp))

这就是说:获取id_src_node的字典,并从该字典中获取list的{​​{1}},然后在该列表后附加一个数字。

但是,'timestamp'上没有添加任何字典,id_src_node上也肯定没有关键字dict_node的列表。

尽管有两种方法可以做到这一点。有dict.setdefault,我不喜欢它的名称或语法,也可以创建一个defaultdict。如果密钥不存在,timestamp将为密钥创建特定类型的对象。

外部字典需要默认的字典,而内部字典需要默认的列表:

defaultdict

然后,当您访问新键时,from collections import defaultdict def inner_dict(): return defaultdict(list) dict_node = defaultdict(inner_dict) 将添加一个dict_node,当您访问内键时,将创建一个空列表,您可以将其高兴地附加到以下内容:

inner_dict

如果您尝试打印默认词典,它们将不像普通的>>> dict_node['02141592cc0000000100000000000000']['timestamp'].append(1234.5) 一样整齐。

要改为使用普通的dictdict

dict.setdefault

您可以看到它很丑陋,但是您会拥有想要的dict_node.setdefault(id_src_node, {}).setdefault("timestamp", []).append(float(timestamp))