File size: 1,114 Bytes
c61bca2 f5f55b8 c61bca2 f5f55b8 c61bca2 f5f55b8 c61bca2 f5f55b8 c61bca2 f5f55b8 c61bca2 f5f55b8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import re
from tname import *
from Rfile import *
def Sort_Scores(file):
scores = []
contents = j_reads(file.name)
for i in range(2, len(contents)):
if i % 2 == 0:
# 使用正则表达式
content = re.match('.*score=(\d.\d+?),', contents[i])
if content:
score = content.group(1)
scores.append(float(score))
na = Name()
na = na + r"scores_sort.tsv" # 结果文件名称
# 按列表scores中元素的值进行排序,并返回元素对应索引序列
sorted_id = []
sorted_id = sorted(range(len(scores)), key=lambda k: scores[k], reverse=True)
# 第一条序列和其他序列格式不一样,且第一条序列不需要排序,单独写入
with open(na, "a") as f1:
f1.write(contents[0])
f1.write(contents[1])
for i in range(0, len(scores)):
with open(na, "a") as f:
f.write(contents[sorted_id[i] * 2 + 2])
# 由于文件前两行未参与排序,所以索引要+2
f.write(contents[sorted_id[i] * 2 + 2 + 1])
return na
|