【发布时间】:2015-04-16 13:18:29
【问题描述】:
我想对包含以下内容的文本文件进行排序 像
这样的行1000S 00RR: 20 values
1200S -10RR: 10 values
900S -20RR: 6 values
150S -05RR: 4 values
10000S 00RR: 2 values
我想将其排序为(按照升序,考虑空间数值之前的第一个元素)
150S -05RR: 4 values
900S -20RR: 6 values
1000S 00RR: 20 values
1200S -10RR: 10 values
10000S 00RR: 2 values
我想知道实现它的更好方法是什么。
我尝试了以下方法:
with open(file_name, "r") as file_name_opened:
lines = file_name_opened.readlines()
for x in range(0,20):
try:
list_one.append(lines[x])
except IndexError:
pass
return sorted(list_one)
print("sorted: " + str(sorted(list_one)))
很高兴知道是否有更好的方法来做到这一点......
【问题讨论】:
-
您是否尝试过任何方法来实现它?您当前的代码是否存在效率问题?
-
@jonrsharpe 我已经添加了我为此所做的实现......
-
那行得通吗?是否效率低下(您是否遇到过性能问题,并进行了任何分析以找出位置)?
标签: python python-3.x text