Python 查找列表的平均值

Python 查找列表的平均值,python,list,lambda,average,reduce,Python,List,Lambda,Average,Reduce,我必须找到Python中列表的平均值。这是到目前为止我的代码 l = [15, 18, 2, 36, 12, 78, 5, 6, 9] print reduce(lambda x, y: x + y, l) 我得到了它,因此它将列表中的值相加,但我不知道如何将其划分为多个值?在Python 3.4+上,您可以使用 在较早版本的Python上,您可以执行以下操作 sum(l) / len(l) 在Python2上,需要将len转换为浮点以获得浮点除法 sum(l) / float(len(l)

我必须找到Python中列表的平均值。这是到目前为止我的代码

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print reduce(lambda x, y: x + y, l)

我得到了它,因此它将列表中的值相加,但我不知道如何将其划分为多个值?

在Python 3.4+上,您可以使用

在较早版本的Python上,您可以执行以下操作

sum(l) / len(l)
在Python2上,需要将
len
转换为浮点以获得浮点除法

sum(l) / float(len(l))

无需使用
reduce
。它的速度要慢得多,而且是在Python3中。

在Python3.4+上,您可以使用

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
sum(l) / len(l)
在较早版本的Python上,您可以执行以下操作

sum(l) / len(l)
在Python2上,需要将
len
转换为浮点以获得浮点除法

sum(l) / float(len(l))

无需使用
reduce
。它的速度要慢得多,而且是在Python3中。

当Python有一个完美的cromultent
sum()函数时,为什么要使用
reduce()

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
sum(l) / len(l)
print sum(l) / float(len(l))

(强制Python进行浮点除法必须使用
float()

当Python有一个完美的cromultent
sum()函数时,为什么要使用
reduce()

print sum(l) / float(len(l))
(强制Python进行浮点除法需要
float()

或者像以前贴的那样

sum(l)/(len(l)*1.0)
1.0是为了确保得到浮点除法

sum(l) / float(len(l))
或者像以前贴的那样

sum(l)/(len(l)*1.0)

1.0是为了确保你得到一个浮点除法

为了使用
reduce
来计算运行平均值,你需要跟踪到目前为止看到的元素总数和总数。由于这在列表中不是一个微不足道的元素,您还必须传递一个额外的参数来折叠

sum(l) / float(len(l))
>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> running_average = reduce(lambda aggr, elem: (aggr[0] + elem, aggr[1]+1), l, (0.0,0))
>>> running_average[0]
(181.0, 9)
>>> running_average[0]/running_average[1]
20.111111111111111

为了使用
reduce
获取运行平均值,您需要跟踪到目前为止看到的元素总数和总数。由于这在列表中不是一个微不足道的元素,您还必须传递一个额外的参数来折叠

>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> running_average = reduce(lambda aggr, elem: (aggr[0] + elem, aggr[1]+1), l, (0.0,0))
>>> running_average[0]
(181.0, 9)
>>> running_average[0]/running_average[1]
20.111111111111111
sum(l)/float(len(l))
是正确的答案,但为了完整性,您可以使用单个reduce计算平均值:

>>> reduce(lambda x, y: x + y / float(len(l)), l, 0)
20.111111111111114
请注意,这可能会导致轻微的舍入误差:

>>> sum(l) / float(len(l))
20.111111111111111
sum(l)/float(len(l))
是正确的答案,但为了完整性,您可以使用单个reduce计算平均值:

>>> reduce(lambda x, y: x + y / float(len(l)), l, 0)
20.111111111111114
请注意,这可能会导致轻微的舍入误差:

>>> sum(l) / float(len(l))
20.111111111111111
您可以使用:

您可以使用:

已经完成了一个模块。它有一个函数来计算调用的平均值。您提供的列表示例如下:

from statistics import mean
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
mean(l)
已经完成了一个模块。它有一个函数来计算调用的平均值。您提供的列表示例如下:

from statistics import mean
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
mean(l)

您可以向总和中添加0.0,而不是强制转换为浮点:

def avg(l):
    return sum(l, 0.0) / len(l)

您可以向总和中添加0.0,而不是强制转换为浮点:

def avg(l):
    return sum(l, 0.0) / len(l)

在一个大学的问题中,我有一个类似的问题要解决。而不是我编码的内置函数:

def list_mean(n):

    summing = float(sum(n))
    count = float(len(n))
    if n == []:
        return False
    return float(summing/count)

时间比平时长得多,但对于初学者来说,这是一个相当具有挑战性的问题。

我在一个大学的问题中有一个类似的问题要解决。而不是我编码的内置函数:

def list_mean(n):

    summing = float(sum(n))
    count = float(len(n))
    if n == []:
        return False
    return float(summing/count)

比平常长得多,但对于初学者来说,这是一个相当具有挑战性的问题。

这两种方法都可以为您提供接近整数或至少10个十进制值的类似值。但如果您真的考虑长浮点值,则两者可能会有所不同。方法可能因您想要实现的目标而异

>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> print reduce(lambda x, y: x + y, l) / len(l)
20
>>> sum(l)/len(l)
20
浮动值

>>> print reduce(lambda x, y: x + y, l) / float(len(l))
20.1111111111
>>> print sum(l)/float(len(l))
20.1111111111

@安德鲁·克拉克的说法是正确的。

这两种方法都能给出接近整数的相似值或至少10个十进制值。但如果您真的考虑长浮点值,则两者可能会有所不同。方法可能因您想要实现的目标而异

>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> print reduce(lambda x, y: x + y, l) / len(l)
20
>>> sum(l)/len(l)
20
浮动值

>>> print reduce(lambda x, y: x + y, l) / float(len(l))
20.1111111111
>>> print sum(l)/float(len(l))
20.1111111111

@安德鲁·克拉克的说法是正确的。

我尝试使用上述选项,但没有成功。 试试这个:

从统计导入平均值
n=[11,13,15,17,19]
打印(n)
打印(平均值(n))

在Python3.5上工作时,我尝试使用上面的选项,但没有成功。 试试这个:

从统计导入平均值
n=[11,13,15,17,19]
打印(n)
打印(平均值(n))

在Python3.5上工作时,结合上述两个答案,我提出了以下与reduce一起使用的方法,并且不假设reduce函数中有可用的
L

from operator import truediv

L = [15, 18, 2, 36, 12, 78, 5, 6, 9]

def sum_and_count(x, y):
    try:
        return (x[0] + y, x[1] + 1)
    except TypeError:
        return (x + y, 2)

truediv(*reduce(sum_and_count, L))

# prints 
20.11111111111111

结合以上两个答案,我得出了以下与reduce一起使用的方法,并且不假设reduce函数中有可用的
L

from operator import truediv

L = [15, 18, 2, 36, 12, 78, 5, 6, 9]

def sum_and_count(x, y):
    try:
        return (x[0] + y, x[1] + 1)
    except TypeError:
        return (x + y, 2)

truediv(*reduce(sum_and_count, L))

# prints 
20.11111111111111

作为初学者,我刚刚编写了以下代码:

L = [15, 18, 2, 36, 12, 78, 5, 6, 9]

total = 0

def average(numbers):
    total = sum(numbers)
    total = float(total)
    return total / len(numbers)

print average(L)

作为初学者,我刚刚编写了以下代码:

L = [15, 18, 2, 36, 12, 78, 5, 6, 9]

total = 0

def average(numbers):
    total = sum(numbers)
    total = float(total)
    return total / len(numbers)

print average(L)

我只想添加另一种方法

import itertools,operator
list(itertools.accumulate(l,operator.add)).pop(-1) / len(l)

我只想添加另一种方法

import itertools,operator
list(itertools.accumulate(l,operator.add)).pop(-1) / len(l)
假设

x = [
    [-5.01,-5.43,1.08,0.86,-2.67,4.94,-2.51,-2.25,5.56,1.03],
    [-8.12,-3.48,-5.52,-3.78,0.63,3.29,2.09,-2.13,2.86,-3.33],
    [-3.68,-3.54,1.66,-4.11,7.39,2.08,-2.59,-6.94,-2.26,4.33]
]
您可以注意到,
x
的维度为3*10,如果您需要为每一行输入
平均值

theMean = np.mean(x1,axis=1)
不要忘记
将numpy作为np导入

假设

x = [
    [-5.01,-5.43,1.08,0.86,-2.67,4.94,-2.51,-2.25,5.56,1.03],
    [-8.12,-3.48,-5.52,-3.78,0.63,3.29,2.09,-2.13,2.86,-3.33],
    [-3.68,-3.54,1.66,-4.11,7.39,2.08,-2.59,-6.94,-2.26,4.33]
]
您可以注意到,
x
的维度为3*10,如果您需要为每一行输入
平均值

theMean = np.mean(x1,axis=1)

不要忘记将numpy作为np导入

如果您使用的是python>=3.4,那么就有一个统计库

你可以用这种卑鄙的方法。假设您有一个要查找的数字列表,其意思是:-

list = [11, 13, 12, 15, 17]
import statistics as s
s.mean(list)

它还有其他方法,如stdev、方差、mode、调和平均值、中值等,这些方法非常有用。

如果您使用的是python>=3.4,则有一个统计库

你可以用这种卑鄙的方法。假设您有一个要查找的数字列表,其意思是:-

list = [11, 13, 12, 15, 17]
import statistics as s
s.mean(list)
它还有其他方法,如标准差分法、方差法、模式法、调和平均法、中值法等,这些方法非常有用。

如果你想得到的不仅仅是平均值(又称平均值),你可以查看scipy统计数据 如果你想得到的不仅仅是平均值(又称平均值),你可以查看scipy统计数据
或者使用
pandas
系列。平均值
方法:

pd.Series(sequence).mean()
演示:

>>> import pandas as pd
>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> pd.Series(l).mean()
20.11111111111111
>>> 
从文档中:

系列。平均值(轴=无,skipna=无,水平=无