Reputation: 269
I need to compute the means of of the first elements in each list, the second elemnts in each list etc and get a list of these means. So i need to get a list d = [5/3, 10/3, ...etc from the following
$
a = [1,2,2,2,3,4,3]
b = [2,3,1,9,5,4,6]
c = [2,5,6,7,8,2,4]
can numpy do this in some way?
Upvotes: 3
Views: 174
Reputation: 25813
Since the question has a numpy tag, I thought I would add a numpy answer too:
numpy.mean([a, b, c], axis=0)
Upvotes: 3
Reputation: 13410
You can do it without NumPy:
>>> map(lambda x: sum(x)/3., zip(a,b,c))
[1.6666666666666667, 3.3333333333333335, 3.0, 6.0, 5.333333333333333, 3.3333333333333335, 4.333333333333333]
Upvotes: 2
Reputation: 71004
Just zip
them together.
>>> list(zip((1, 2, 3), (1,2,3), (1,2,3)))
[(1, 1, 1), (2, 2, 2), (3, 3, 3)]
From there, you can iterate over the result and take the means as you need them.
Upvotes: 2