Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:对于列表列表,获取每个位置的平均值_Python - Fatal编程技术网

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

Python:对于列表列表,获取每个位置的平均值,python,Python,我有一份清单: 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)

我有一份清单:

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)
这个职位很有意义。我想返回一个列表,其中包含每个位置的平均值,不包括-1。也就是说,我想要:

[(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]
实际上是:

[0.37, 0.5866666666666667, 0.515, 0.10500000000000001, 0.9]
我怎样才能用蟒蛇的方式来做呢

编辑:

我正在使用Python2.7,我不是在寻找每个列表的平均值;相反,我在寻找“位置0处的所有列表元素不包括-1”的平均值,以及“位置1处的所有列表元素不包括-1”的平均值,等等

我的理由是:

[(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]

位置0的值是-1、-1、0.54和0.2,我想排除-1;位置1有0.67、0.24和0.85;位置3有0.23、-1和0.8等。无第三方库的解决方案:

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)]
它使用with fillvalue设置为-1来转换列表,并将缺少的值设置为-1将在下一步被忽略。然后,一个生成器表达式和用于过滤掉-1并获得平均值。

这里是一个基于矢量化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]

您可以使用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)))

你应该研究一下列表的理解。类似于[suml/lenl for l in list of the list]的东西与过滤器相结合,以删除您不想要的任何特定内容,这将是一个良好的开端。@ap和神奇的拉链当然。@ap谢谢,但它是位置方面的,而不是列表方面的。列表中l的suml/lenl会给出每个列表的平均值,这不是我想要的。啊,我误解了“位置”是指主列表中的位置。您可能希望按照bipll的建议使用zip。我应该在问题中提到:我正在使用Python 2.7,而zip_longest只能在Python 3中工作。这个答案的一个问题是:如果b.shape[0]-neg_count[I]为0,则在该空间中返回nan。在那种情况下,我希望它返回-1。这里是修订版:means=[np.sumb[:,i]/b.shape[0]-neg_count[i]如果b.shape[0]-neg_count[i]!=0,则在范围b.shape[1]中,如果i为其他-1,则你会有一个全为-1的列吗?当然,您的更新是有效的,但它会降低效率。[除了您不需要其他0而不是其他-1吗?]是的,这是可能的-我从答案中返回了一些NaN,因此尽管这效率较低,但我认为这是必要的。我可以使用else 0,但在我的上下文中使用else-1是有意义的,因为在我的代码中,这是一个不可能的数字表示,我永远不会有负的平均值。
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)))