首页 > 解决方案 > 如何将文本文件中的数字从小到大排序?

问题描述

高分.txt

16
23
17
15
60
40
13

这就是我所拥有的

Top_Score = open("highscore.txt", "r+")
for line in Top_Score.readlines():
    print(line)

Top_Score.close()

最好只打印前 5 个最小的数字。任何帮助,将不胜感激。

样本输出

13
15
16
17
23
40
60

标签: pythonpython-3.x

解决方案


In [85]: data = sorted(map(int, open("highscore.txt")))

In [86]: data
Out[86]: [13, 15, 16, 17, 23, 40, 60]

推荐阅读