我必须计算以下列表的平均值:
j=[20, 30, 40, None, 50]
此外,这些嵌套列表中的最小值也包含相同的值:
x = [[20, 30, 40, None, 50], [12, 31, 43, None, 51]]
哪个应该返回[12,30,40,50]
但是以下代码不起作用。
print(list(map(min, zip(*x))))
TypeError: '<' not supported between instances of 'NoneType' and 'NoneType'
对于平均值我试过这个:
import statistics
statistics.harmonic_mean(j)
他们都没有在这种类型的列表上工作。
您可以过滤掉“无”值以获得均值和分钟。例如:
from statistics import mean
data = [[20, 30, 40, None, 50], [12, 31, 43, None, 51]]
mean_val = mean(d for d in data[0] if d is not None)
print(mean_val)
# 35
min_vals = [min(a, b) for a, b in zip(*data) if a is not None and b is not None]
print(min_vals)
# [12, 30, 40, 50]