【发布时间】:2018-07-11 11:51:00
【问题描述】:
我有 2 个默认 dict.. 我想将这 2 个结合起来。 请帮帮我。
{ Fun :{ 1:hi , 2: hello} , fun2 : {3: bye, 4: good bye}}
{Fun :{ 1:abc , 2: xyZ} , fun2 : {3: qpr, 4: jkl}}
我想将这些组合起来,在键 1 下得到 'hi' 和 'abc' 以及 'fun'
【问题讨论】:
-
"'hi' and 'abc' " - 连接在一起还是作为一个列表?还是别的什么?
-
在python中,字典键是唯一的。这意味着您只能在同一个字典中使用一个“Fun”。
-
标准库中没有这种东西。不过,您可以使用 defaultdict(docs.python.org/3.1/library/…)。示例代码:- >>> from collections import defaultdict >>> md = defaultdict(list) >>> md[1].append('a') >>> md[1].append('b') >> > md[2].append('c') >>> md[1] // 输出为 ['a', 'b'] >>> md[2] // 输出为 ['c']
标签: python dictionary