Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the sum of subsets of a list in python

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?

like image 794
fitzgeraldsteele Avatar asked May 26 '11 03:05

fitzgeraldsteele


1 Answers

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))
like image 104
Winston Ewert Avatar answered Sep 23 '22 02:09

Winston Ewert