想要在整数和浮点数之间排序,整数从最低到最高排序

我有一个包含以下数据的文件:


1,0.00008118

1,0.00008101

12,0.00008122

5,0.00008091

8,0.00008092

1,0.00008109

2,0.00008100

260,0.00008087

248,0.00007982

69,0.00007958

71,0.00007954

51,0.00007910

20,0.00007932

我希望能够对文件进行排序,以便将整数值从最低到最高排序。使用 sorted 函数进行排序时,它似乎是按数字排序。


a_s = sorted(List)

for i in a_s:

    print(i)

所以输出是:


1,0.00008101

1,0.00008109

1,0.00008118

12,0.00008122

2,0.00008100

20,0.00007932

248,0.00007982

260,0.00008087

5,0.00008091

51,0.00007910

69,0.00007958

71,0.00007954

8,0.00008092

我将如何获得它以便将数值从最低值到最高值排序?


30秒到达战场
浏览 90回答 1
1回答

斯蒂芬大帝

尝试这个:with open('test.txt') as fp:    data = [line.strip().split(',') for line in fp]res = '\n'.join([','.join(x) for x in sorted(data, key=lambda x: int(x[0]))])print(res)输出:1,0.000081181,0.000081011,0.000081092,0.000081005,0.000080918,0.0000809212,0.0000812220,0.0000793251,0.0000791069,0.0000795871,0.00007954248,0.00007982260,0.00008087
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python