Python Forum

Full Version: Newbie question for a kind of rolling average of list of lits
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello:
I have one list of lists, like this:
totals = [[1, 2, 3], [2, 3, 4], [5, 6, 7], [10, 9, 8], [4, 5, 6]]
I need a function to get a kind of rolling average.
The rule is: for every 2 elements in the totals list, find the average of the both elements and save them in another list of lists:
In my above example, I want to get this:
averages = [ [1.5, 2.5, 3.5], [3.5, 4.5, 5.5], [7.5, 8.0, 7.5], [7.0, 7.0, 7.0]]
The first one: [1.5, 2.5, 3.5] is the average of [[1, 2, 3], [2, 3, 4]]
The second one: [3.5, 4.5, 5.5] is the average of [[2, 3, 4], [5, 6, 7]]
The third one: [7.5, 8.0, 7.5] is the average of [[5, 6, 7], [10, 9, 8]]
The last one: [7.0, 7.0, 7.0] is the average of [[10, 9, 8], [4, 5, 6]]
Please advise,
Thanks,
Looks like a homework assignment, right.
This sounds like a homework. Do you have any intentions to show us what you have tried so far? Put the code between code tags please - BBcode
i wish they would stop assigning lists-of-lists homework. lists of tuples, dictionaries of lists, but lists of lists? thats just setting them up.