简体   繁体   English

基于字典中的一个键的字典列表中一个值的总和

[英]sum of one value in the list of dictionary based on one key in dict

I want to sum one value in the list of dictionary based on another key value is equal. 我想根据另一个键值等于等于字典列表中的一个值。

stackOverflow much easier question answer for just sum the total value: I have a very big list of dictionaries and I want to sum the insides 只求总值的总和就容易了stackOverflow问题答案: 我有一个很大的词典列表,我想对内部求和

For example: if we have 例如:如果我们有

lst = [{'year': 2013, 'snow': 64.8, 'month': 1},
       {'year': 2013, 'snow': 66.5, 'month': 2},
       {'year': 2013, 'snow': 68.3, 'month': 12},
       {'year': 2013, 'snow': 68.8, 'month': 3},
       {'year': 2013, 'snow': 70.9, 'month': 11},
       {'year': 2012, 'snow': 76.8, 'month': 7},
       {'year': 2012, 'snow': 79.6, 'month': 5},
       {'year': 1951, 'snow': 86.6, 'month': 12}]

to get the sum of snow fall in that year: 得到当年降雪的总和:

the output should: 输出应为:

snowfall = [{'year': 2013, 'totalsnow': 339.3},
            {'year': 2012, 'totalsnow': 156.4},
            {'year': 1951, 'totalsnow': 86.6}]

Here is my code: 这是我的代码:

for i in range(len(lst)):
        while lst[i]['year']:
            sum(value['snow'] for value in lst)

then it will goes wrong, output 然后会出错,输出

582.3000000000001

How to get it right? 如何正确处理? Please be sample and explain as well. 请样本并解释。 I am new to python. 我是python的新手。

Use a dictionary to track snow-per-year; 使用字典来追踪每年的降雪量; a collections.defaultdict() object is ideal here: 一个collections.defaultdict()对象在这里很理想:

from collections import defaultdict

snowfall = defaultdict(float)

for info in lst:
    snowfall[info['year']] += info['snow']

snowfall = [{'year': year, 'totalsnow': snowfall[year]} 
            for year in sorted(snowfall, reverse=True)]

This first creates a defaultdict() object that'll create new float() objects (value 0.0) for keys that don't exist yet. 这首先创建一个defaultdict()对象,该对象将为尚不存在的键创建新的float()对象(值0.0)。 It sums the values per year for you. 它汇总了您每年的价值。

The last lines create your desired structure, sorted by year in descending order. 最后几行创建所需的结构,并按年份降序排列。

Demo: 演示:

>>> from collections import defaultdict
>>> lst = [{'year': 2013, 'snow': 64.8, 'month': 1},
...        {'year': 2013, 'snow': 66.5, 'month': 2},
...        {'year': 2013, 'snow': 68.3, 'month': 12},
...        {'year': 2013, 'snow': 68.8, 'month': 3},
...        {'year': 2013, 'snow': 70.9, 'month': 11},
...        {'year': 2012, 'snow': 76.8, 'month': 7},
...        {'year': 2012, 'snow': 79.6, 'month': 5},
...        {'year': 1951, 'snow': 86.6, 'month': 12}]
>>> snowfall = defaultdict(float)
>>> for info in lst:
...     snowfall[info['year']] += info['snow']
... 
>>> snowfall = [{'year': year, 'totalsnow': snowfall[year]} 
...             for year in sorted(snowfall, reverse=True)]
>>> from pprint import pprint
>>> pprint(snowfall)
[{'totalsnow': 339.30000000000007, 'year': 2013},
 {'totalsnow': 156.39999999999998, 'year': 2012},
 {'totalsnow': 86.6, 'year': 1951}]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在dicts列表中,根据一个键/值匹配dict? - In list of dicts, match dict based on one key/value? 追加词典列表,在一个词典中附加键,并以嵌套字典作为其值 - Appending list of dictionaries with an additional key in one dictionary and a nested dict as its value 通过将一个 dict 列表的值作为键添加到另一个 dict 将两个 dict 列表组合到一个 dict - Combine two list of dict to one dict by adding value of one list of dict as key to another Python - 基于相同的键对字典列表中的值求和 - Python - Sum the value in the list of dictionary based on the same key 根据同一个字典中的另一个,提取熊猫中一个字典键的值 - Extracting value for one dictionary key in Pandas based on another in the same dictionary 合并两个列表以基于一个列表的元素作为键(具有重复项)和其他作为值来制作字典 - Merge two list to make a dictionary based on elements of one list as key (with duplicates) and other as value 如何将字典中的值求和每个键 - How to sum values in dict one key each 一行返回列表中第一个字典中包含另一个键值的字典键的值 - one line to return value of dict key in first dict in list that contains another key value 有没有办法根据一个字典中的值小于另一个字典中的相同键来过滤字典列表? - Is there a way to filter a list of dictionaries based on a value in one dictionary being less than the same key in another? 一个字典的类似字典的字符串列表 - list of dict-like strings to one dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM