Find the sum of subsets of a list in python

fitzgeraldsteele picture fitzgeraldsteele · May 26, 2011 · Viewed 27.5k times · Source

This is probably very simple and I'm overlooking something...

I have a long list of integers, in this case representing daily visitors to a website. I want a new list of weekly visitors. So I need to get groups of seven from the original list, sum them, and add them to a new list.

My solution seems pretty brute force, inelegant:

numweeks = len(daily) / 7
weekly = []
for x in range(numweeks):
    y = x*7
    weekly.append(sum(visitors[y:y+7]))

Is there a more efficient, or more pythonic way of doing this?

Answer

Winston Ewert picture Winston Ewert · May 26, 2011
weekly = [ sum(visitors[x:x+7]) for x in range(0, len(daily), 7)]

Or slightly less densely:

weekly = []
for x in range(0, len(daily), 7):
     weekly.append( sum(visitors[x:x+7]) )

Alternatively, using the numpy module.

by_week = numpy.reshape(visitors, (7, -1))
weekly = numpy.sum( by_week, axis = 1)

Note that this requires the number of elements in visitor be a multiple of 7. It also requires that you install numpy. However, its probably also more efficient then the other approaches.

Or for itertools code bonus:

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return itertools.izip_longest(fillvalue=fillvalue, *args)

weekly = map(sum, grouper(7, visitors, 0))