1

I am still having troubles adjusting to 'more pythonian ways' of writing code sometimes ... right now I am iterating over some values (x). I have many arrays and I always compare the first value of all the arrays, the second value ... shortly: a mean value of all the entries in an array by position in the array.

   sum_mean_x = []
   for i in range(0, int_points):
       for j in range(0, len(x)):
           mean_x.append(x[j][i])
       sum_mean_x.append(sum(mean_x)/len(x))
       mean_x = []

I am pretty sure that can be done super beautiful. I know I could change the second last line to something like sum_mean_x.append(mean_x.mean) but, I guess I miss some serious magic this way.

2
  • Can you add an example of int_points and x? Commented Apr 30, 2015 at 9:01
  • First, the most pythonic way to get the mean of something is probably to call statistics.mean if you have some kind of Python iterable like a list, or the ndarray.mean method if you actually have (NumPy) arrays. Commented Apr 30, 2015 at 9:02

4 Answers 4

6

Use the numpy package for numeric processing. Suppose you have the following three lists in plain Python:

a1 = [1., 4., 6.]
a2 = [3., 7., 3.]
a3 = [2., 0., -1.]

And you want to get the mean value for each position. Arrange the vectors in a single array:

import numpy as np
a = np.array([a1, a2, a3])

Then you can get the per-column mean like this:

>>> a.mean(axis=0)
array([ 2.        ,  3.66666667,  2.66666667])
Sign up to request clarification or add additional context in comments.

Comments

3

It sounds like what you're trying to do is treat your list of lists are a 2D array where each list is a row, and then average each column.

The obvious way to do this is to use NumPy, make it an actual 2D array, and just call mean by columns. See simleo's answer, which is better than what I was going to add here. :)

But if you want to stick with lists of lists, going by column effectively means transposing, and that means zip:

>>> from statistics import mean
>>> arrs = [[1., 2., 3.], [0., 0., 0.], [2., 4., 6.]]
>>> column_means = [mean(col) for col in zip(*arrs)]
>>> column_means
[1.0, 2.0, 3.0]

That statistics.mean is only in the stdlib in 3.4+, but it's based on stats on PyPI, and if yur Python is too old even for that, you can write it on your own. Getting the error handling right on the edge cases is tricky, so you probably want to look at the code from statistics, but if you're only dealing with values near 1, you can just do it the obvious way:

def mean(iterable):
    total, length = 0.0, 0
    for value in iterable:
        total += value
        length += 1
    return total / length

2 Comments

+1 you beat me to the exact same answer. I was almost writing at the zip(*arrs) part. Strangely, I was using the exact same variable name arrs!
@ComputerFellow: There have been at least like 5 questions in the past hour where zip was the core of the answer, and at least 2 of them used arrs as a variable name (and all of them referred to "arrays" despite actually having lists) so maybe that's why it was in both our heads?
0
ar1 = [1,2,3,4,5,6]
ar2 = [3,5,7,2,5,7]

means = [ (i+j)/2.0 for (i,j) in zip(ar1, ar2)]
print(means)

2 Comments

The question says "I have many arrays." So an answer that doesn't scale to more than 2 probably isn't helpful.
Your true I misunderstood the question.
-1

You mean something like

import numpy as np    

ar1 = [1,2,3,4,5,6]
ar2 = [3,5,7,2,5,7]

mean_list = []
for i, j in zip(ar1, ar2):
    mean_list.append(np.array([i,j]).mean())

print(mean_list)
[2.0, 3.5, 5.0, 3.0, 5.0, 6.5]

1 Comment

Mixing NumPy and iteration like this is usually not a good idea. Once you bring in NumPy, you can do the whole thing in NumPy a lot more simply (and efficiently, if that matters), as simleo's answer shows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.