jie_test4 / Sort_Scores.py
jie1's picture
Update Sort_Scores.py
59aeb49
raw
history blame
1.15 kB
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])
results = j_reads(na)
return na, results