python中的分组字典键值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18180145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:06:56  来源:igfitidea点击:

Group dictionary key values in python

pythonpython-2.7dictionary

提问by Alchemist777

I have the following dictionary

我有以下字典

mylist = [{'tpdt': '0.00', 'tmst': 45.0, 'tmdt': 45.0, 'pbc': 30, 'remarks': False, 'shift': 1, 'ebct': '0.00', 'tmcdt': '0.00', 'mc_no': 'KA20'}, 
          {'tpdt': '0.00', 'tmst': 45.0, 'tmdt': 45.0, 'pbc': 30, 'remarks': False, 'shift': 1, 'ebct': '0.00', 'tmcdt': '0.00', 'mc_no': 'KA20'}, 
          {'tpdt': '0.00', 'tmst': 55.0, 'tmdt': 55.0, 'pbc': 30, 'remarks': False, 'shift': 1, 'ebct': '0.00', 'tmcdt': '0.00', 'mc_no': 'KA23'}, 
          {'tpdt': '0.00', 'tmst': 55.0, 'tmdt': 55.0, 'pbc': 30, 'remarks': False, 'shift': 1, 'ebct': '0.00', 'tmcdt': '0.00', 'mc_no': 'KA23'}]

I want to get the sum of the key 'tmst' for every dictionary values 'KA20' and 'KA23' in the list of dictionaries.

我想获得字典列表中每个字典值 'KA20' 和 'KA23' 的键 'tmst' 的总和。

Could you please have your suggestions on this??

请问您对此有什么建议吗??

采纳答案by alecxe

You can use itertools.groupby:

您可以使用itertools.groupby

>>> for key, group in itertools.groupby(mylist, lambda item: item["mc_no"]):
...     print key, sum([item["tmst"] for item in group])
... 
KA20 90.0
KA23 110.0

Note that for groupbyto work properly, mylisthas to be sorted by the grouping key:

请注意,为了groupby正常工作,mylist必须按分组键排序:

from operator import itemgetter

mylist.sort(key=itemgetter("mc_no"))

回答by Milan Thakkar

First you needs to sort that list of dictionaries..using following code.. Ex.

首先,您需要对字典列表进行排序..使用以下代码..例如。

    animals = [{'name':'cow', 'size':'large'},{'name':'bird', 'size':'small'},{'name':'fish', 'size':'small'},{'name':'rabbit', 'size':'medium'},{'name':'pony', 'size':'large'},{'name':'squirrel', 'size':'medium'},{'name':'fox', 'size':'medium'}]

    import itertools
    from operator import itemgetter
    sorted_animals = sorted(animals, key=itemgetter('size'))

then you use following code

然后你使用以下代码

    for key, group in itertools.groupby(sorted_animals, key=lambda x:x['size']):
        print key,
        print list(group)

After you will get following result...

得到以下结果后...

large [{'name': 'cow', 'size': 'large'}, {'name': 'pony', 'size':'large'}]
medium [{'name': 'rabbit', 'size': 'medium'}, {'name': 'squirrel', 'size':'medium'}, {'name': 'fox', 'size': 'medium'}]
small [{'name': 'bird', 'size': 'small'}, {'name': 'fish', 'size': 'small'}]

回答by jfs

Here's a code that doesn't require mylistto be sorted by "mc_no"key:

这是一个不需要按键mylist排序的代码"mc_no"

from collections import defaultdict

sums = defaultdict(int)  # key -> sum
for d in mylist:
    sums[d["mc_no"]] += d["tmst"]