python - 如何将文本文件中的数字从小到大排序?
问题描述
高分.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
解决方案
In [85]: data = sorted(map(int, open("highscore.txt")))
In [86]: data
Out[86]: [13, 15, 16, 17, 23, 40, 60]
推荐阅读
- regex - 用于匹配特定数字模式的正则表达式
- javascript - 如何使用 JS 选择一个选择框的选项值?
- android - api.ai 进程开始监听时录制音频
- java - 如何创建一个对应于从 2 到 100 的每个数字的列表,该列表将包含数组中作为其倍数的数字?
- ios - 快速停止用户交互时滚动视图中的自动滚动
- admin - 我为菜单分组配置的奏鸣曲管理员有什么问题?
- java - Spring MongoDB Aggregation Group - 无法正确获取组查询
- excel - 自动完成
- facebook - 为什么当我尝试获取我个人受邀参加的任何 Facebook 活动的“所有者”时,facebook 的图形 API 会返回权限错误?
- websocket - 跨域时如何解决和解决 SockJs 的 /info 路径上的 404