trans_out_res.txt
[67545 69287 82972 70845 80996]
[142522 90580 97826 97562 72554]
[302981 254754 308544 273743 134552]
[139220 75252 72992 12517 90535]
[ 31490 299477 299478 233707 316110]
list_res = []
with open("../TransH/trans_out_res.txt", 'r', encoding='utf-8') as fo:
sentence = fo.readline()
while sentence:
sentence = sentence.replace("\n", "")
sentence = sentence.replace("]", "")
sentence = sentence.replace("[", "")
tmp = sentence.split()
for num in tmp:
list_res.append(int(num))
sentence = fo.readline()
print(list_res)
排序:
# 将数字和对应重复次数作为key,value放入字典
a = {}
for i in list_res:
if list_res.count(i) >= 1:
a[i] = list_res.count(i)
# 对字典value(重复次数)降序排序,以列表形式返回
a = sorted(a.items(), key=lambda item: item[1], reverse=True)
# [(5, 6), (1, 3), (2, 2), (4, 2)]
num = []
# 获取列表中每个元组中的第一个值,即数组中的数字(此时已是按重复次数降序)
for item in a:
num.append(item[0])
print(num)