python求列表均值
时间: 2024-01-03 19:21:25 浏览: 87
python笔记之mean()函数实现求取均值的功能代码
5星 · 资源好评率100%
以下是三种Python求列表均值的方法:
1.使用Python内置函数sum()和len()求列表均值
```python
numbers = [1, 2, 3, 4, 5]
mean = sum(numbers) / len(numbers)
print(mean) # 输出 3.0
```
2.使用numpy库求列表均值
```python
import numpy as np
numbers = [1, 2, 3, 4, 5]
mean = np.mean(numbers)
print(mean) # 输出 3.0
```
3.使用自定义函数求列表均值
```python
def mean(numbers):
return sum(numbers) / len(numbers)
numbers = [1, 2, 3, 4, 5]
print(mean(numbers)) # 输出 3.0
```
阅读全文