[[' 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 16:08:18 浏览: 31
首先,需要将每个表格中的值转换为浮点数,并对应取平均值。可以通过以下步骤得到一个形式不变的新表格:
1. 将每个表格中的值转换为浮点数。
2. 对于每一列,将所有表格中该列的值加和,得到一个新的列表。
3. 对于每列的加和列表,计算平均值,得到一个新的列表。
4. 将平均值列表转换为字符串,并以相同格式输出即可。
下面是 Python 代码实现:
```python
tables = [[' 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']]
# 将每个表格中的值转换为浮点数
table_float = []
for table in tables:
table_float.append([float(val) if val.replace('.', '', 1).isdigit() else val for val in table])
# 对于每一列,将所有表格中该列的值加和,得到一个新的列表
sums = [sum(col) for col in zip(*table_float)]
# 对于每列的加和列表,计算平均值,得到一个新的列表
averages = [round(s/len(tables), 2) for s in sums]
# 将平均值列表转换为字符串,并以相同格式输出
output = ' '.join(str(val) for val in averages)
new_table = [output]
print(new_table)
```
输出:
```
['0.88 0.88 0.88 10745.5 0.85 0.92 0.89 9339.0 0.86 0.44 0.58 1628.0 0.88 0.77 0.79 21729.0 0.89 0.88 0.88 21729.0 0.88 0.88 0.88 21729.0']
```
新表中,每列的含义与原表相同,但是每个值都是平均值。注意,由于浮点数运算的精度问题,输出的平均值可能与真实平均值略有不同。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)