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?
Or slightly less densely:
Alternatively, using the numpy module.
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: