需求
做实验的需求,字典转json保存到文件中,再从json文件中读取数据,转为字典,再将带图结构的数据存入字典,最后按key排序输出。
实现
test = {}
testr = {}
for u in [2,4,1,0,3]:
for r in range(2):
testiv = {}
for i,v in zip([100,101,102],[200,201,202]):
testiv[i]=v
testr[r]=testiv
test[u] =testr
print("字典结构:")
print(test)
import json
filename = 'test1.json'
with open(filename, 'w', encoding='UTF-8') as f:
json.dump(test, f, ensure_ascii=False)
print("保存成功!")
# 从文件读取JSON格式的字符串,并将其转化为字典
with open(filename, 'r', encoding='UTF-8') as f:
global aa
aa = json.load(f)
print("读取JSON文件中的内容:")
print(aa)
uriv = defaultdict(list)
for u in aa.keys():
for r in aa['0'].keys():
ckg_graph = nx.MultiDiGraph()
for key,value in zip(aa[u][r].keys(),aa[u][r].values()):
ckg_graph.add_edge(key, value, key=r)
uriv[u].append(ckg_graph)
print("带图结构的:")
print(uriv)
print("调整顺序后:")
for i in sorted (uriv) :
print ((i, uriv[i]), end =" ")
结果:
字典结构:
{2: {0: {100: 200, 101: 201, 102: 202}, 1: {100: 200, 101: 201, 102: 202}}, 4: {0: {100: 200, 101: 201, 102: 202}, 1: {100: 200, 101: 201, 102: 202}}, 1: {0: {100: 200, 101: 201, 102: 202}, 1: {100: 200, 101: 201, 102: 202}}, 0: {0: {100: 200, 101: 201, 102: 202}, 1: {100: 200, 101: 201, 102: 202}}, 3: {0: {100: 200, 101: 201, 102: 202}, 1: {100: 200, 101: 201, 102: 202}}}
保存成功!
读取JSON文件中的内容:
{'2': {'0': {'100': 200, '101': 201, '102': 202}, '1': {'100': 200, '101': 201, '102': 202}}, '4': {'0': {'100': 200, '101': 201, '102': 202}, '1': {'100': 200, '101': 201, '102': 202}}, '1': {'0': {'100': 200, '101': 201, '102': 202}, '1': {'100': 200, '101': 201, '102': 202}}, '0': {'0': {'100': 200, '101': 201, '102': 202}, '1': {'100': 200, '101': 201, '102': 202}}, '3': {'0': {'100': 200, '101': 201, '102': 202}, '1': {'100': 200, '101': 201, '102': 202}}}
带图结构的:
defaultdict(<class 'list'>, {'2': [<networkx.classes.multidigraph.MultiDiGraph object at 0x000002C175C83CD0>, <networkx.classes.multidigraph.MultiDiGraph object at 0x000002C175CF4130>], '4': [<networkx.classes.multidigraph.MultiDiGraph object at 0x000002C179191310>, <networkx.classes.multidigraph.MultiDiGraph object at 0x000002C179191400>], '1': [<networkx.classes.multidigraph.MultiDiGraph object at 0x000002C179191640>, <networkx.classes.multidigraph.MultiDiGraph object at 0x000002C1791914F0>], '0': [<networkx.classes.multidigraph.MultiDiGraph object at 0x000002C179191520>, <networkx.classes.multidigraph.MultiDiGraph object at 0x000002C179191580>], '3': [<networkx.classes.multidigraph.MultiDiGraph object at 0x000002C1791915B0>, <networkx.classes.multidigraph.MultiDiGraph object at 0x000002C1791912B0>]})
调整顺序后:
('0', [<networkx.classes.multidigraph.MultiDiGraph object at 0x000002C179191520>, <networkx.classes.multidigraph.MultiDiGraph object at 0x000002C179191580>]) ('1', [<networkx.classes.multidigraph.MultiDiGraph object at 0x000002C179191640>, <networkx.classes.multidigraph.MultiDiGraph object at 0x000002C1791914F0>]) ('2', [<networkx.classes.multidigraph.MultiDiGraph object at 0x000002C175C83CD0>, <networkx.classes.multidigraph.MultiDiGraph object at 0x000002C175CF4130>]) ('3', [<networkx.classes.multidigraph.MultiDiGraph object at 0x000002C1791915B0>, <networkx.classes.multidigraph.MultiDiGraph object at 0x000002C1791912B0>]) ('4', [<networkx.classes.multidigraph.MultiDiGraph object at 0x000002C179191310>, <networkx.classes.multidigraph.MultiDiGraph object at 0x000002C179191400>])