python求平均值函数
时间: 2023-10-13 20:06:32 浏览: 42
以下是一个Python函数,用于计算一个数字列表的平均值:
```python
def mean(numbers):
return sum(numbers) / len(numbers)
```
使用示例:
```python
>>> numbers = [2, 3, 5, 7, 11]
>>> mean(numbers)
5.6
```
相关问题
python列表求平均值函数
我们可以使用Python内置的函数或第三方库numpy来计算列表的平均值。
1. 使用Python内置的函数sum()和len()来计算列表的平均值,具体实现代码如下:
```python
my_list = [1, 2, 3, 4, 5]
average = sum(my_list) / len(my_list)
print("The average of the list is:", average) # 输出:3.0
```
2. 使用第三方库numpy中的mean()函数来计算列表的平均值,具体实现代码如下:
```python
import numpy as np
my_list = [1, 2, 3, 4, 5]
average = np.mean(my_list)
print("The average of the list is:", average) # 输出:3.0
```
python求平均值的函数
Python求平均值的函数是mean(),可以使用numpy库中的mean()函数或者使用Python自带的statistics库中的mean()函数来实现。例如,使用numpy库中的mean()函数可以这样写:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
result = np.mean(arr)
print(result)
输出结果为:
3.0