List python中DICT列表中特定键的求和值

List python中DICT列表中特定键的求和值,list,python-2.7,dictionary,List,Python 2.7,Dictionary,这是一个让我头疼的小问题。我有一个目录,如下所示: [{'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

这是一个让我头疼的小问题。我有一个目录,如下所示:

[{'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}
 ]
我想做的是汇总每种药物不同目录下的总处方,并将最终金额作为条目添加到每个目录中,如下所示:

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

完成此操作的最有效方法是什么?

从集合导入计数器

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

从收款进口柜台

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}]
[{'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}]
这样的事情应该是相当有效的

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
像这样的东西应该相当有效。

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
这是相当有效的,因为它只对列表中的每个dict循环一次,然后对每个不同的med名称循环一次


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

使用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
输出:

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

使用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
输出:

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

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