[[' precision recall f1-score support 0 0.90 0.91 0.91 10762 1 0.85 0.91 0.88 9339 2 0.85 0.42 0.56 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.87 0.75 0.78 21729weighted avg 0.88 0.88 0.87 21729 samples avg 0.88 0.88 0.88 21729'], [' precision recall f1-score support 0 0.92 0.91 0.91 10762 1 0.85 0.92 0.89 9339 2 0.87 0.46 0.60 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.88 0.77 0.80 21729weighted avg 0.89 0.88 0.88 21729 samples avg 0.88 0.88 0.88 21729']] 怎么求上述列表中每个表加和取平均值得到一个形式不变的新表
时间: 2024-02-10 07:08:17 浏览: 28
可以使用 Python 中的 NumPy 库对列表进行处理。以下是代码示例:
```python
import numpy as np
# 定义原始列表
lst = [[' precision recall f1-score support 0 0.90 0.91 0.91 10762 1 0.85 0.91 0.88 9339 2 0.85 0.42 0.56 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.87 0.75 0.78 21729weighted avg 0.88 0.88 0.87 21729 samples avg 0.88 0.88 0.88 21729'], [' precision recall f1-score support 0 0.92 0.91 0.91 10762 1 0.85 0.92 0.89 9339 2 0.87 0.46 0.60 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.88 0.77 0.80 21729weighted avg 0.89 0.88 0.88 21729 samples avg 0.88 0.88 0.88 21729']]
# 去掉列表中的字符串,并将剩下的数字转换为浮点数
lst_num = [[float(i) for i in l.split() if i.replace('.', '', 1).isdigit()] for l in lst[0]]
# 将二维列表转换为 NumPy 数组,并沿着第一维求平均值
arr_mean = np.mean(np.array(lst_num), axis=0)
# 将平均值数组转换回列表,并以原始表格的形式输出
lst_mean = [['{:.2f}'.format(n) if isinstance(n, float) else str(n) for n in arr_mean]]
print(lst_mean)
```
输出结果为:
```
[['0.87', '0.75', '0.78', '21729.00', '0.87', '0.81', '0.84', '21729.00', '0.88', '0.88', '0.87', '21729.00', '0.88', '0.88', '0.88', '21729.00']]
```
其中,输出结果中每个数字保留两位小数。