【发布时间】:2015-01-17 23:07:08
【问题描述】:
我在单独的行中给出了这个输入,我想创建一个嵌套图。
输入:
a b 3
b c 4
a c 10
所需的嵌套字典:
{'a': {
'b': 3,
'c': 10 },
'b': {'c': 4 }
}
这是我的代码,但是它覆盖了相同的键。
for rec in range(2, num_lines):
first, second, length = lines[rec].split()
d[first] = {}
d[first][second] = length
【问题讨论】:
-
使用哪种语言?蟒蛇?
-
是的。 Python。谢谢。
标签: python dictionary nested key overwrite