python mean of list of lists python mean of list of lists numpy numpy

python mean of list of lists


You could use the following:

listA = [[2,3,-7,-4], [-2,3,4,-5], [-5,-6,-8,2], [9,5,13,2]]means = [np.mean([el for el in sublist if el < 0] or 0) for sublist in listA]print(means)

Output

[-5.5, -3.5, -6.3333, 0.0]

If none of the elements in sublist are less than 0, the list comprehension will evaluate to []. By including the expression [] or 0 we handle your scenario where you want to evaluate the mean of an empty list to be 0.


If you're using numpy at all, you should strive for numpythonic code rather than falling back to python logic. That means using numpy's ndarray data structure, and the usual indexing style for arrays, rather than python loops.

For the usual means:

>>> listA[[2, 3, -7, -4], [-2, 3, 4, -5], [-5, -6, -8, 2], [9, 5, 13, 2]]>>> A = np.array(listA)>>> np.mean(A, axis=1)array([-1.5 ,  0.  , -4.25,  7.25])

Negative means:

>>> [np.mean(row[row<0]) for row in A][-5.5, -3.5, -6.333333333333333, nan]


The pure numpy way :

In [2]: np.ma.masked_greater(listA,0).mean(1).dataOut[2]: array([-5.5       , -3.5       , -6.33333333,  0.        ])