Python 访问字典中的子字典中的值

Python 访问字典中的子字典中的值,python,dictionary,data-structures,Python,Dictionary,Data Structures,你好,我有一本字典,看起来像这样: dictionary = {'John': {'car':12, 'house':10, 'boat':3}, 'Mike': {'car':5, 'house':4, 'boat':6}} cars_total = dictionary['car'] house_total = dictionary['house'] boat_total = dictionary['boat'] 我希望获得访问权限并提取子字典中的键,并将它们

你好,我有一本字典,看起来像这样:

dictionary = {'John': {'car':12, 'house':10, 'boat':3},
              'Mike': {'car':5, 'house':4, 'boat':6}}
cars_total = dictionary['car']
house_total = dictionary['house']
boat_total = dictionary['boat']
我希望获得访问权限并提取子字典中的键,并将它们分配给如下变量:

dictionary = {'John': {'car':12, 'house':10, 'boat':3},
              'Mike': {'car':5, 'house':4, 'boat':6}}
cars_total = dictionary['car']
house_total = dictionary['house']
boat_total = dictionary['boat']
现在,当我运行上面的变量时,我得到一个“键错误”。这是可以理解的,因为我需要首先访问外部字典。如果有人帮助我如何访问子字典中的键和值,我将不胜感激,因为这些是我想要使用的值

此外,我还想创建一个新密钥,这可能不正确,但在以下几行:

    car = dictionary['car']
    house = dictionary['house']
    boat = dictionary['boat']

dictionary['total_assets'] = car + house + boat 
我希望能够访问字典中的所有键并创建一个新键。外部键(如“John”和“Mike”)的末尾都应包含新制作的键。
我知道这会带来一个错误,但它会让您了解我想要实现的目标。感谢您的帮助,汇总每个人的总资产,并将其添加为一个新的关键字:

for person in dictionary:
    dictionary[person]['total_assets'] = sum(dictionary[person].values())
这将导致:

dictionary = {'John': {'car':12, 'house':10, 'boat':3, 'total_assets':25},
              'Mike': {'car':5, 'house':4, 'boat':6, 'total_assets':15}}

如你所见,
dictionary
没有钥匙
car
,但是
dictionary['John']
有钥匙

$ >>> dictionary['John']
{'car': 12, 'boat': 3, 'house': 10}
>>> dictionary['John']['car']
12
>>> 
字典
中的每个键相关联的值本身就是另一个字典,您可以分别对其进行索引。没有单个对象包含每个子字典的
car
值;您必须迭代 超过每个值

# Iterate over the dictionary once per aggregate
cars_total = sum(d['car'] for d in dictionary.values())
house_total = sum(d['house'] for d in dictionary.values())
boat_total = sum(d['boat'] for d in dictionary.values())


示例迭代方法:

from collections import defaultdict
totals = defaultdict(int)
for person in dictionary:
    for element in dictionary[person]:
        totals[element] += dictionary[person][element]

print(totals)
输出:

defaultdict(<type 'int'>, {'car': 17, 'boat': 9, 'house': 14})
defaultdict(,{'car':17,'boat':9,'house':14})

我只需要使用
计数器
对象来获取总数:

>>> from collections import Counter
>>> totals = Counter()
>>> for v in dictionary.values():
...     totals.update(v)
...
>>> totals
Counter({'car': 17, 'house': 14, 'boat': 9})
>>> totals['car']
17
>>> totals['house']
14
>>>
这样做还有一个额外的好处,就是即使键并不总是存在,也能很好地工作

如果需要总资产,则可以简单地将值相加:

>>> totals['total_assets'] = sum(totals.values())
>>> totals
Counter({'total_assets': 40, 'car': 17, 'house': 14, 'boat': 9})
>>>

dictionary['John']['car']
?但我只能猜测,您正在尝试对外部字典中所有键的值求和?是否可以通过使用循环来完成此操作?@juanpa.arrivillagaa内部键始终可用?为什么不直接编写您需要的结果want@DeepakM看到我的答案了。这真是太好了。这里的柜台真的很方便…:D@repzero赞成h、
计数器
通常非常方便。正如最初所问,这是最好的解决方案。另外还有一位代表,他对自己的真实问题进行了推敲,最终投票确定了OP想要什么。很抱歉,很难解释,但仍然感谢您的帮助!@juanpa。arrivillaga@DeepakM下次再复习吧会更清楚。@TemporalWolf出于好奇。如果我想让“总资产”只包含“汽车”和“房子”的总和,那怎么办?
sum(dictionary[person]中(key,value)的值)。items()如果key!=“boat”)