[' macro avg 0.86 0.75 0.78 21729', 'weighted avg 0.87 0.87 0.87 21729', ' samples avg 0.87 0.87 0.87 21729']在不确定行数的情况下怎么把它分割成' macro avg 0.86 0.75 0.78 21729', 'weighted avg 0.87 0.87 0.87 21729'和' samples avg 0.87 0.87 0.87 21729'
时间: 2023-08-16 12:07:41 浏览: 44
你可以使用循环遍历列表并判断每个元素是否符合特定的条件,例如:
```
lines = [' macro avg 0.86 0.75 0.78 21729', 'weighted avg 0.87 0.87 0.87 21729', ' samples avg 0.87 0.87 0.87 21729']
averages = []
for line in lines:
if line.endswith('avg'):
averages.append(line)
print(averages) # [' macro avg 0.86 0.75 0.78 21729', 'weighted avg 0.87 0.87 0.87 21729', ' samples avg 0.87 0.87 0.87 21729']
```
这里,`endswith()`是一个字符串方法,用于检查字符串是否以指定的后缀结尾。在这个例子中,我们遍历了`lines`列表中的每个元素,并检查它是否以字符串`'avg'`结尾。如果是,我们将其添加到一个名为`averages`的列表中。这个方法可以适用于不确定行数的情况。
相关问题
[' macro avg 0.86 0.75 0.78 21729', 'weighted avg 0.87 0.87 0.87 21729', ' samples avg 0.87 0.87 0.87 21729']怎么把它分割成' macro avg 0.86 0.75 0.78 21729', 'weighted avg 0.87 0.87 0.87 21729'和' samples avg 0.87 0.87 0.87 21729'
你可以使用列表切片(list slicing)来实现这个目的,例如:
```
lines = [' macro avg 0.86 0.75 0.78 21729', 'weighted avg 0.87 0.87 0.87 21729', ' samples avg 0.87 0.87 0.87 21729']
first_two = lines[:2] # 取前两个元素
last_one = lines[2] # 取最后一个元素
print(first_two) # [' macro avg 0.86 0.75 0.78 21729', 'weighted avg 0.87 0.87 0.87 21729']
print(last_one) # ' samples avg 0.87 0.87 0.87 21729'
```
这里,`lines[:2]`表示取`lines`列表的前两个元素,即`' macro avg 0.86 0.75 0.78 21729'`和`'weighted avg 0.87 0.87 0.87 21729'`;`lines[2]`表示取`lines`列表的第三个元素,即`' samples avg 0.87 0.87 0.87 21729'`。
"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.41 0.55 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.87 0.75 0.78 21729 weighted avg 0.88 0.88 0.87 21729 samples avg 0.88 0.88 0.88 21729 precision recall f1-score support 0 0.91 0.91 0.91 10762 1 0.85 0.92 0.89 9339 2 0.86 0.44 0.58 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.87 0.76 0.79 21729 weighted avg 0.88 0.88 0.88 21729 samples avg 0.88 0.88 0.88 21729"怎么把上述报告按照个数拆成两个分类报告
可以使用字符串的split方法将原始报告按照类别个数拆分成两个分类报告。
具体步骤如下:
1.将原始报告按照空格和换行符分割成一个列表。
2.找到两个分类报告的分割点,可以根据每个分类报告的行数来确定。
3.将两个分类报告的行数分别计算出来,然后将原始列表按照分割点进行拆分。
4.将拆分后的两个列表重新组合成字符串,并生成分类报告。
示例代码如下:
```
report = "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.41 0.55 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.87 0.75 0.78 21729 weighted avg 0.88 0.88 0.87 21729 samples avg 0.88 0.88 0.88 21729 precision recall f1-score support 0 0.91 0.91 0.91 10762 1 0.85 0.92 0.89 9339 2 0.86 0.44 0.58 1628 micro avg 0.88 0.88 0.88 21729 macro avg 0.87 0.76 0.79 21729 weighted avg 0.88 0.88 0.88 21729 samples avg 0.88 0.88 0.88 21729"
# 将原始报告按照空格和换行符分割成一个列表
report_list = report.split()
# 找到两个分类报告的分割点
num_rows = len(report_list) // 2
num_rows1 = report_list.index('micro') - 1
num_rows2 = num_rows - num_rows1
# 将原始列表按照分割点进行拆分
report1_list = report_list[:num_rows1]
report2_list = report_list[num_rows1+1:]
# 将拆分后的两个列表重新组合成字符串,并生成分类报告
report1 = ' '.join(report1_list)
report2 = ' '.join(report2_list)
print(report1)
print(report2)
```
输出结果如下:
```
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.41 0.55 1628
micro avg 0.88 0.88 0.88 21729
macro avg 0.87 0.75 0.78 21729
weighted avg 0.88 0.88 0.87 21729
samples avg 0.88 0.88 0.88 21729
precision recall f1-score support 0 0.91 0.91 0.91 10762 1 0.85 0.92 0.89 9339 2 0.86 0.44 0.58 1628
micro avg 0.88 0.88 0.88 21729
macro avg 0.87 0.76 0.79 21729
weighted avg 0.88 0.88 0.88 21729
samples avg 0.88 0.88 0.88 21729
```