【问题标题】:Summing values for a particular key in a list of dicts in python在python中的dicts列表中对特定键的值求和
【发布时间】:2016-03-01 17:10:30
【问题描述】:

这是一个棘手的小问题,我被困住了。我有一个字典列表,如下所示:

[{'medication_name': 'Actemra IV',
  'total_prescriptions': 4},
 {'medication_name': 'Actemra IV',
  'total_prescriptions': 3},
 {'medication_name': 'Actemra IV',
  'total_prescriptions': 1},
 {'medication_name': 'Actemra IV',
  'total_prescriptions': 6},
 {'medication_name': 'Actemra SC',
  'total_prescriptions': 8},
 {'medication_name': 'Actemra SC',
  'total_prescriptions': 1},
 {'medication_name': 'Actemra SC',
  'total_prescriptions': 3}]

我想要做的是总结每种药物的各种字典的总处方,并将最终总和作为每个字典的条目附加如下:

[{'medication_name': 'Actemra IV',
  'total_prescriptions': 4,
  'final_count': 14},
 {'medication_name': 'Actemra IV',
  'total_prescriptions': 3,
  'final_count': 14},
 {'medication_name': 'Actemra IV',
  'total_prescriptions': 1,
  'final_count': 14},
 {'medication_name': 'Actemra IV',
  'total_prescriptions': 6,
  'final_count': 14},
 {'medication_name': 'Actemra SC',
  'total_prescriptions': 8,
  'final_count': 12},
 {'medication_name': 'Actemra SC',
  'total_prescriptions': 1,
  'final_count': 12},
 {'medication_name': 'Actemra SC',
  'total_prescriptions': 3,
  'final_count': 12}
 ]

最有效的方法是什么?

【问题讨论】:

  • 你为什么选择了另一个答案,和我的一样,只是慢了点?
  • 哦,我看到他对其进行了编辑以纠正其不必要的复杂性。

标签: list python-2.7 dictionary


【解决方案1】:

从集合导入计数器

counts = Counter()
for dct in lst:
    counts[dct['medication_name']] += dct['total_prescriptions']
for dct in lst:
    dct['final_count'] = counts[dct['medication_name']]

from pprint import pprint    as pp

输出:

[{'final_count': 14,
  'medication_name': 'Actemra IV',
  'total_prescriptions': 4},
 {'final_count': 14,
  'medication_name': 'Actemra IV',
  'total_prescriptions': 3},
 {'final_count': 14,
  'medication_name': 'Actemra IV',
  'total_prescriptions': 1},
 {'final_count': 14,
  'medication_name': 'Actemra IV',
  'total_prescriptions': 6},
 {'final_count': 12,
  'medication_name': 'Actemra SC',
  'total_prescriptions': 8},
 {'final_count': 12,
  'medication_name': 'Actemra SC',
  'total_prescriptions': 1},
 {'final_count': 12,
  'medication_name': 'Actemra SC',
  'total_prescriptions': 3}]

【讨论】:

    【解决方案2】:
    from collections import defaultdict
    tot = defaultdict(int)
    for d in your_dict_list:
        tot[d['medication_name']] += d['total_prescriptions']
    
    for d in your_dict_list:
        d['final_count'] = tot[d['medication_name']]
    

    类似的东西应该是相当有效的。

    【讨论】:

      【解决方案3】:

      from collections import defaultdict
      
      
      dlist = [{'medication_name': 'Actemra IV',
        'total_prescriptions': 4},
       {'medication_name': 'Actemra IV',
        'total_prescriptions': 3},
       {'medication_name': 'Actemra IV',
        'total_prescriptions': 1},
       {'medication_name': 'Actemra IV',
        'total_prescriptions': 6},
       {'medication_name': 'Actemra SC',
        'total_prescriptions': 8},
       {'medication_name': 'Actemra SC',
        'total_prescriptions': 1},
       {'medication_name': 'Actemra SC',
        'total_prescriptions': 3}]
      
      
      indexes = defaultdict(list)
      
      for i in xrange(0, len(dlist)):
          indexes[dlist[i]['medication_name']].append(i)
      
      for med_k, med_list in indexes.iteritems():
          tot = sum([dlist[i]['total_prescriptions'] for i in med_list])
          for i in med_list:
              dlist[i]['final_count'] = tot
      

      这是相当有效的,因为它只循环列表中的每个字典一次,然后每个不同的医学名称循环一次

      【讨论】:

        【解决方案4】:

        使用 groupby 和 itemgetter:

        from itertools import groupby
        from operator import itemgetter
        L = [ (i['medication_name'],i['total_prescriptions']) for i in dlist]
        sum_dict = dict([(x, sum(map(itemgetter(1), y))) for x, y in groupby(L, itemgetter(0))])
        for i, v in enumerate(dlist): dlist[i]['final_count'] = sum_dict[v['medication_name']]
        print dlist
        

        输出:

        [{'medication_name': 'Actemra IV', 'final_count': 14, 'total_prescriptions': 4}, 
        {'medication_name': 'Actemra IV', 'final_count': 14, 'total_prescriptions': 3}, 
        {'medication_name': 'Actemra IV', 'final_count': 14, 'total_prescriptions': 1}, 
        {'medication_name': 'Actemra IV', 'final_count': 14, 'total_prescriptions': 6}, 
        {'medication_name': 'Actemra SC', 'final_count': 12, 'total_prescriptions': 8}, 
        {'medication_name': 'Actemra SC', 'final_count': 12, 'total_prescriptions': 1}, 
        {'medication_name': 'Actemra SC', 'final_count': 12, 'total_prescriptions': 3}]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-25
          • 1970-01-01
          • 1970-01-01
          • 2021-02-08
          • 2015-07-03
          • 2014-05-31
          • 2021-05-22
          相关资源
          最近更新 更多