在Python中计算列表中元素的平均值,你可以使用以下几种方法:
```python
def mean_with_loop(numbers):
total = 0
for n in numbers:
total += n
return total / len(numbers)
2. 使用Python内置的`sum`函数:```pythondef mean_with_sum(numbers):
return sum(numbers) / len(numbers)
3. 使用`numpy`库中的`mean`函数:

```python
import numpy as np
def mean_with_numpy(numbers):
return np.mean(numbers)
4. 使用`numpy`库计算2D列表的平均值(例如矩阵):```pythonimport numpy as np
def mean_2d_with_numpy(matrix, axis=0):
return np.mean(matrix, axis=axis)
你可以根据你的需要选择合适的方法来计算列表的平均值。
