<p>根据你的字典变得有多疯狂,以及你想获得这种类型的统计信息的粒度,你可能想研究重构你的结构,以使用<code>pandas</code>,这为数据操作、切片打开了更多的大门,然而对于初学者来说,它确实有一点学习曲线,所以我将避免通常的建议“嘿,使用这个库”。如果您感兴趣,可以阅读一些文档,<a href="https://pandas.pydata.org/docs/" rel="nofollow noreferrer">https://pandas.pydata.org/docs/</a></p>
<p>要直接回答您的问题,您可能需要重新构建total sales字典。您可以使用一个“total_sales_by_make_and_model”字典,然后使用<code>cars</code>列表中列出的make键作为该字典的键--这会使它更清晰一些,但是我不确定这与您的整体“业务”逻辑有什么关系</p>
<p>我还建议将代码扩展一点,以将键名存储在变量中,至少在看到求和出错的地方之前是这样</p>
<pre><code>cars = [
{'id': 2, 'car': {'car_make':'Acura', 'car_model':'TL', 'car_year':2005},'total_sales':589},
{'id': 30, 'car': {'car_make':'Acura', 'car_model':'TL', 'car_year':2004}, 'total_sales':167},
{'id': 31, 'car': {'car_make':'Acura', 'car_model':'Integra', 'car_year':2008}, 'total_sales':200},
{'id': 71, 'car': {'car_make':'BMW', 'car_model':'5 Series', 'car_year':2011},'total_sales':824},
{'id': 72, 'car': {'car_make':'BMW', 'car_model':'5 Series', 'car_year':2001}, 'total_sales':6}
]
total_sales_by_make_and_model = {}
for car in cars:
car_make = car['car']['car_make']
car_model = car['car']['car_model']
total = car['total_sales']
# if the make key doesn't exist, create it
if car_make not in total_sales_by_make_and_model:
total_sales_by_make_and_model[car_make] = {}
# use get to set the value to "total" if the key doesn't exist
# otherwise this will increment the current value by "total"
total_sales_by_make_and_model[car_make][car_model] = total_sales_by_make_and_model[car_make].get(car_model, 0) + total
</code></pre>
<p>此结果将使您能够灵活地在make键中按模型进行查询。例如:</p>
<pre><code>print(total_sales_by_make_and_model['Acura'])
print(total_sales_by_make_and_model['BMW']['5 Series'])
</code></pre>
<p>输出:</p>
<pre><code>{'TL': 756, 'Integra': 200}
830
</code></pre>
<p>如果您想获得每个品牌的总销售额,可以使用一个简单的循环遍历每个键并对值求和:</p>
<pre><code>for make in total_sales_by_make_and_model:
total = sum(total_sales_by_make_and_model[make].values())
print(f"Total sales for '{make}': {total}")
</code></pre>
<p>输出:</p>
<pre><code>Total sales for 'Acura': 956
Total sales for 'BMW': 830
</code></pre>
<p><strong>编辑:</strong>必须修复几个给出错误金额的主要打字错误:)</p>