开发者

I have a very big list of dictionaries and I want to sum the insides

Something like

{A: 3, 45, 34, 4, 2, 5, 94, 2139, 230345, 283047, 230847}, {B: 92374, 324, 345, 345, 45879, 34857987, 34579开发者_运维问答38457), {C: 23874923874987, 2347}

How can I reduce that to

{A: 2304923094820398}, {B: 2374923784897}, {C: 29348239847239847}

Values obviously not exact. I just want to add up all the values in a non-time consuming manner.


d = [{'A': [3, 45, 34, 4, 2, 5, 94, 2139, 230345, 283047, 230847]}, {'B': [92374, 324, 345, 345, 45879, 34857987, 3457938457]}, {'C': [23874923874987, 2347]}]
[{x.keys()[0]:sum(x.values()[0])} for x in d]


Something like this:

a={'a':range(100), 'b':range(200)}
b={}

for k,v in a.iteritems():
    b[k]=sum(v)

print b

gives

{'a': 4950, 'b': 19900}


Assuming the values are in a list, like:

d = {A: [3, 45, 34, 4, 2, 5, 94, 2139, 230345, 283047, 230847]}, {B: [92374, 324, 345, 345, 45879, 34857987, 3457938457]), {C: [23874923874987, 2347]}

then:

new_d = []
for i in d:
    new_d.append(dict([(k,sum(v)) for k,v in i.itervalues()]))


If you have lots and lots and lots of values, something like this might work (assuming by "list of dictionaries" you mean "dictionary of lists", as having a big list of one-key/value-pair dictionaries would be silly, and thus the values are stored in a dict like {'A': [3, 45, 34, 4, 2, 5, 94, 2139, 230345, 283047, 230847], 'B': [92374, 324, 345, 345, 45879, 34857987, 3457938457], ...}):

from multiprocessing import Process, Lock

def sumfunc(lock, key, values):
    total = sum(values)

    lock.acquire()
    print(key + ':', total)   # The actual code would be better off utilizing shared multiprocessing.Array objects, but I haven't used them before so am not sure quite how you set them up so decided to leave that aside for now as this is just a theoretical example.
    lock.release()

if __name__ == '__main__':
    lock = Lock()

    for key, values in dict_of_values_to_sum.items():
        Process(target=sumfunc, args=(lock, key, values)).start()


If you have a real big number of values i'll probably use numpy and do a vector addition.

So, something like

 a = numpy.arange(10, 2000000)
 total = numpy.sum(a)

Nothing more, nothing less :)


d = [{'A': [3, 45, 34, 4, 2, 5, 94, 2139, 230345, 283047, 230847]}, {'B': [92374, 324, 345, 345, 45879, 34857987, 3457938457]}, {'C': [23874923874987, 2347]}]
abc = {}
for x in d:
    for k, v in x.iteritems():
        abc[k] = reduce(lambda x,y: x+y, v)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜