2011-05-21 135 views
2

我想要在字典中有值的总和。
以下是我写的代码。字典中的值的总和

results = collections.defaultdict(dict) 
for main, month, tot in list_data: 
    d = results[main] 
    d[month] = tot 
    d.setdefault('total', 0) 
    d['total'] += tot 
result_output = dict(results) 

上面的代码,给出以下输出继电器:

{u'Apple': {'January': 17, 'February': 1, 'total': 19, 'March': 1}, 
u'Oranges': {'total': 1, 'March': 1}, 
u'Graphes': {'January': 24, 'February': 1, 'total': 66, 'March': 41}} 

但我想这样的输出:

{u'Apple': {'January': 17, 'February': 1, 'total': 19, 'March': 1}, 
u'Oranges': {'total': 1, 'March': 1}, 
u'Graphes': {'January': 24, 'February': 1, 'total': 66, 'March': 41, 'April': 1}, 
u'grandtotal': {'January': 41 , 'February': 3, 'March': 43, 'April':1 }} 

我只是想知道,如果有人可以帮助我解决这个问题我有。我真的很感激。

+1

一般情况下,如果你能给样本数据会有所帮助;即使它只说三项。 – 2011-05-21 21:39:03

回答

1

这个怎么样? (未测试)

from collections import defaultdict 
from functools import partial 

results = defaultdict(partial(defaultdict, int)) 
for main, month, tot in list_data: 
    results[main][month] += tot 
    results[main]["total"] += tot 
    results[u"grandtotal"][month] += tot 
result_output = dict((k, dict(v)) for k, v in results.items()) 

EDIT:result_output现在有字典值而不是defaultdict值。

+0

如何在这个字典中做订单,就像水果应该先来到grandtotal? – newbe 2011-05-21 22:10:12

+0

@newbe:python'dict's是无序的字典,所以你不能通过'dict's来改变'grand_total'之前'水果'的键的顺序。有些修改过的词典类是在新版本的python中有序的词典(例如,python 2.7/3.1)有'collections.OrderedDict' http://docs.python.org/dev/library/collections.html#collections.OrderedDict和你可以为老版本的python安装类似的配方(例如'ordereddict.OrderedDict' http://www.xs4all.nl/~anthon/Python/ordereddict/)。但他们会有所不同。 – 2011-05-22 01:43:24

0

所以你想添加一个grandtotal。

如果你开始像一个结构:

starting_data = {u'Apple': {'January': 17, 'February': 1, 'total': 19, 'March': 1}, 
       u'Oranges': {'total': 1, 'March': 1}, 
       u'Graphes': {'January': 24, 'February': 1, 'total': 66, 'March': 41}} 

你可以做这样的事情

grand_total = defaultdict(int) # makes it default to 0 
for fruit, fruitdict in starting_data.items(): 
    for month, total in fruitdict.items(): 
     grand_total[month] += total 
starting_data[u'grand_total'] = dict(grand_total) 

这已经过测试,并给出

{u'Apple': {'February': 1, 'January': 17, 'March': 1, 'total': 19}, 
u'Graphes': {'February': 1, 'January': 24, 'March': 41, 'total': 66}, 
u'Oranges': {'March': 1, 'total': 1}, 
u'grand_total': {'February': 2, 'January': 41, 'March': 43, 'total': 86}} 

显然你并不需要再次通过清单并可以更早地汇总;但我喜欢测试并不知道输入数据的格式。

1

你可以试试,没有测试...

gt = collections.defaultdict(int)   # get a new dict 
results = collections.defaultdict(dict) 
for main, month, tot in list_data: 
    d = results[main] 
    d[month] = tot 
    gt[month]+=tot       # populate it 
    d.setdefault('total', 0) 
    d['total'] += tot 
result_output = dict(results) 
results_output['grand_total'] = gt  # save it 
0

使用collections.Counter:

import collections 

results = collections.Counter() 

a = {u'Apple': {'January': 17, 'February': 1, 'total': 19, 'March': 1}, 
    u'Oranges': {'total': 1, 'March': 1}, 
    u'Graphes': {'January': 24, 'February': 1, 'total': 66, 'March': 41}} 

for counts in a.values(): 
    results.update(counts) 

print results # Counter({'total': 86, 'March': 43, 'January': 41, 'February': 2})