简体   繁体   English

Python:对于列表列表,获取每个位置的平均值

[英]Python: for a list of lists, get mean value in each position

I have a list of lists: 我有一个清单清单:

list_of_lists = []
list_1 = [-1, 0.67, 0.23, 0.11]
list_2 = [-1]
list_3 = [0.54, 0.24, -1]
list_4 = [0.2, 0.85, 0.8, 0.1, 0.9]
list_of_lists.append(list_1)
list_of_lists.append(list_2)
list_of_lists.append(list_3)
list_of_lists.append(list_4)

The position is meaningful. 该职位是有意义的。 I want to return a list that contains the mean per position, excluding -1. 我想返回一个列表,其中包含每个位置的平均值,但不包括-1。 That is, I want: 也就是说,我想要:

[(0.54+0.2)/2, (0.67+0.24+0.85)/3, (0.23+0.8)/2, (0.11+0.1)/2, 0.9/1]

which is actually: 实际上是:

[0.37, 0.5866666666666667, 0.515, 0.10500000000000001, 0.9]

How can I do this in a pythonic way? 如何以pythonic方式执行此操作?

EDIT: 编辑:

I am working with Python 2.7, and I am not looking for the mean of each list; 我正在使用python 2.7,而不是寻找每个列表的均值; instead, I'm looking for the mean of 'all list elements at position 0 excluding -1', and the mean of 'all list elements at position 1 excluding -1', etc. 相反,我正在寻找“位置0处的所有列表元素的平均值,不含-1”和“位置1处的所有列表元素的平均值,不含-1”,等等。

The reason I had: 我的原因是:

[(0.54+0.2)/2, (0.67+0.24+0.85)/3, (0.23+0.8)/2, (0.11+0.1)/2, 0.9/1]

is that values in position 0 are -1, -1, 0.54, and 0.2, and I want to exclude -1; 是位置0的值为-1,-1、0.54和0.2,我想排除-1; position 1 has 0.67, 0.24, and 0.85; 位置1具有0.67、0.24和0.85; position 3 has 0.23, -1, and 0.8, etc. 位置3具有0.23,-1和0.8等。

A solution without third-party libraries: 没有第三方库的解决方案:

from itertools import zip_longest
from statistics import mean

def f(lst):
    return [mean(x for x in t if x != -1) for t in zip_longest(*lst, fillvalue=-1)]
>>> f(list_of_lists)
[0.37, 0.5866666666666667, 0.515, 0.10500000000000001, 0.9]

It uses itertools.zip_longest with fillvalue set to -1 to "transpose" the list and set missing values to -1 (will be ignored at the next step). 它使用itertools.zip_longest并将fillvalue设置为-1来“转置”列表并将缺少的值设置为-1 (在下一步中将被忽略)。 Then, a generator expression and statistics.mean are used to filter out -1 s and get the average. 然后,使用生成器表达式和statistics.mean过滤掉-1 s并获得平均值。

Here is a vectorised numpy -based solution. 这是一个基于矢量的基于numpy的解决方案。

import numpy as np

a = [[-1, 0.67, 0.23, 0.11],
     [-1],
     [0.54, 0.24, -1],
     [0.2, 0.85, 0.8, 0.1, 0.9]]

# first create non-jagged numpy array
b = -np.ones([len(a), max(map(len, a))])

for i, j in enumerate(a):
    b[i][0:len(j)] = j

# count negatives per column (for use later)
neg_count = [np.sum(b[:, i]==-1) for i in range(b.shape[1])]

# set negatives to 0
b[b==-1] = 0

# calculate means
means = [np.sum(b[:, i])/(b.shape[0]-neg_count[i]) \
         if (b.shape[0]-neg_count[i]) != 0 else 0 \
         for i in range(b.shape[1])]

# [0.37,
#  0.58666666666666667,
#  0.51500000000000001,
#  0.10500000000000001,
#  0.90000000000000002]

You can use pandas module to process.Code would like this : 您可以使用pandas模块进行处理。代码如下:

import numpy as np
import pandas as pd

list_1 = [-1, 0.67, 0.23, 0.11,np.nan]
list_2 = [-1,np.nan,np.nan,np.nan,np.nan]
list_3 = [0.54, 0.24, -1,np.nan,np.nan]
list_4 = [0.2, 0.85, 0.8, 0.1, 0.9]
df=pd.DataFrame({"list_1":list_1,"list_2":list_2,"list_3":list_3,"list_4":list_4})
df=df.replace(-1,np.nan)
print(list(df.mean(axis=1)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM