Group dictionary key values in python

后端 未结 3 1917
执念已碎
执念已碎 2020-12-06 10:24

I have the following dictionary

mylist = [{\'tpdt\': \'0.00\', \'tmst\': 45.0, \'tmdt\': 45.0, \'pbc\': 30, \'remarks\': False, \'shift\': 1, \'ebct\': \'0.         


        
3条回答
  •  再見小時候
    2020-12-06 10:33

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

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

提交回复
热议问题