Sum up values in dictionary in Python 2.7
Question: I have a dictionary with data for every 0,04 sec, looking like that

    
    
    {1: 0, 2: 4.22109297745, 3: 0.324239117507, 4: 3.99972239616 ...}
    

keys represent time and values — data I received. I need to count the
arithmetic mean for every second. So first I have to sum up data from every 25
values. And here I'm stuck... I would appreciate a lot some help.


Answer: As I understand your dictionary contains keys 1 to 25 for the 1st sec, 26-50
for the 2nd etc which are inserted in a sequential way... Assuming that, you
could do the following First make an ordered dictionary

    
    
        import collections
        od = collections.OrderedDict(sorted(d.items()))
    

and then

    
    
        avg=[]
        sum = 0
        for k,v in od.iteritems():
            sum += v
            if k%25 == 0:
                avg.append(sum/25)
                sum = 0
    

By the end of the loop avg[0] will contain the average of the 1st second's
values, avg[1] the average of the 2nd etc... However this is going to work
only if every second has exactly 25 values

