运行总计dict列表

时间:2011-12-16 01:39:37

标签: python functional-programming dictionary running-total

有一个dict的python列表如下:

Dict1 = [{'date': 1, 'name': 'xyz', 'qty': 100},
         {'date': 1, 'name': 'xyz', 'qty': 200},
         {'date': 1, 'name': 'xyz', 'qty': 300},
         {'date': 1, 'name': 'xyz2', 'qty': 30},
         {'date': 2, 'name': 'xyz', 'qty': 1000},
         {'date': 2, 'name': 'xyz2', 'qty': 300},
         {'date': 3, 'name': 'xyz', 'qty': 500},
         {'date': 3, 'name': 'xyz2', 'qty': 500},
         {'date': 3, 'name': 'xyz3', 'qty': 500},
         {'date': 3, 'name': 'xyz', 'qty': 600}]

希望为每个日期的每个名称运行总计数量:

date:1,name:xyz,qty:600
date:1,name:xyz2,qty:30
date:2,name:xyz,qty:1600
date:2,name:xyz2,qty:330
date:3,name:xyz,qty:2700
date:3,name:xyz2,qty:830
date:3,name:xyz3,qty:500

感谢。

5 个答案:

答案 0 :(得分:4)

from itertools import groupby
from operator import itemgetter
for k, gr in groupby(Dict1, key=itemgetter('date', 'name')):
    print "date:%i,name:%s,qty:%i" % (k[0], k[1], sum(d['qty'] for d in gr))

答案 1 :(得分:1)

我假设日期正在增加订单。

# store values
tot = {}
# the last date 
date0 = Dict1[-1]['date']

# easier to work from back, i found
for line in Dict1[-1::-1]:
    date, name, qty = [line[x] for x in 'date', 'name', 'qty']

    # add the value to all subsequent days
    for d in range(date, date0+1): 
        tot.setdefault(d, {}).setdefault(name, [0])[0] += qty

# i was putting value into array, and i put it out into a scalar
tot = dict((k, dict((kk,vv[0]) for kk,vv in v.items())) for k,v in tot.items())
print tot

结果:

{1: {'xyz': 600, 'xyz2': 30}, 2: {'xyz': 1600, 'xyz2': 330}, 3: {'xyz': 2700, 'xyz3': 500, 'xyz2': 830}}

答案 2 :(得分:0)

保持总计的简单方法是使用collections.defaultdict

from collections import defaultdict

totals = defaultdict(int)

for d in Dict1:
    name = d['name']
    # increment total
    totals[name] += d['qty']
    print 'date:%s,name:%s,qty:%d' % (d['date'], name, totals[name])

答案 3 :(得分:0)

我发现只有一些复杂的方式:

items = {}
for item in Dict1:
    key = (item['date'], item['name'])
    items.setdefault(key, 0)
    items[key] += item['qty']

Dict2 = sorted([dict(date=key[0], name=key[1], qty=qty) for key, qty in items.items()],
    key=labmda x: (x['date', x['name']))

答案 4 :(得分:0)

result = {}
for date, name in [ (d['date'], d['name']) for d in Dict1]:
    result[(date, name)] = sum( [ d['qty'] for d in Dict1 if d['date'] <= date and d['name'] == name] )
keys = result.keys()
keys.sort()
for key in keys:
    print "date:%d, name:%s, qty:%d"%(key[0], key[-1], result[key])