在python中的dicts列表中对特定键的值求和

时间:2016-03-01 17:10:31

标签: list python-2.7 dictionary

这是一个让我感到困惑的小问题..我有一个dicts列表如下:

none

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

[{'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}]

实现这一目标的最有效方法是什么?

4 个答案:

答案 0 :(得分:0)

来自集合导入计数器

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}]

答案 1 :(得分:0)

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']]

这样的事情应该合理有效。

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

使用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}]