Python:在嵌套字典中为一个键附加多个值

时间:2014-12-26 13:21:43

标签: python dictionary

我有下面的元组列表:

p = [("01","Master"),("02","Node"),("03","Node"),("04","Server")]

我希望我的输出看起来像:

y = {
     "Master":{"number":["01"]},
     "Node":{"number":["02", "03"]},
     "Server":{"number":["04"]}
     }

我尝试过以下代码:

y = {}
for line in p:
     if line[1] in y:
          y[line[1]] = {}
          y[line[1]]["number"].append(line[0])
     else:
          y[line[1]] = {}
          y[line[1]]["number"] = [line[0]]

我得到以下错误:

 Traceback (most recent call last):
         File "<stdin>", line 4, in <module>
 KeyError: 'number'

我该如何解决这个问题?

4 个答案:

答案 0 :(得分:1)

from collections import defaultdict

d = defaultdict(lambda: defaultdict(list))
for v, k in p:
    d[k]["number"].append(v)


print(d)

  defaultdict(<function <lambda> at 0x7f8005097578>, {'Node': defaultdict(<type 'list'>, {'number': ['02', '03']}), 'Master': defaultdict(<type 'list'>, {'number': ['01']}), 'Server': defaultdict(<type 'list'>, {'number': ['04']})})

没有defaultdict:

d = {}
from pprint import pprint as pp
for v, k in p:
    d.setdefault(k,{"number":[]})
    d[k]["number"].append(v)


pp(d)

{'Master': {'number': ['01']},
 'Node': {'number': ['02', '03']},
 'Server': {'number': ['04']}}

答案 1 :(得分:1)

当密钥已经出现在y中时,不要将{}分配给密钥。

y = {}
for line in p:
     try:
          y[line[1]]["number"].append(line[0])
     except:
          y[line[1]] = {}
          y[line[1]]["number"] = [line[0]]

OR 使用defaultdict使用: -

>>> from collections import defaultdict
>>> p = [("01","Master"),("02","Node"),("03","Node"),("04","Server")]
>>> d = defaultdict(list)
>>> for k, v in p:
...    d[v].append(k)
... 
>>> d
defaultdict(<type 'list'>, {'Node': ['02', '03'], 'Master': ['01'], 'Server': ['04']})

答案 2 :(得分:1)

这是因为您不需要在需要时初始化字典,并在不需要时重置字典。

试试这个:

p = [("01","Master"),("02","Node"),("03","Node"),("04","Server")]

y = {}
for (number, category) in p:
    if not y.get(category, False):
        # initializes your sub-dictionary
        y[category] = {"number": []}

    # adds the correct number to the sub-dictionary
    y[category]["number"].append(number)

请注意,使用元组解包for (number, category) in p可以让您的代码在循环中更具可读性。

答案 3 :(得分:1)

您正在重设词典!

for line in p:
     if line[1] in y:
          #y[line[1]] = {}   -- RESET! ["number"] will now disappear.
                               #.. which leads to error in the next line.
          y[line[1]]["number"].append(line[0])
     else:
          y[line[1]] = {}
          y[line[1]]["number"] = [line[0]]

如同其他答案所示,使用defaultdict的更加pythonic方法是实现同样的事情。