我有一个包含以下数据的文件:1,0.000081181,0.0000810112,0.000081225,0.000080918,0.000080921,0.000081092,0.00008100260,0.00008087248,0.0000798269,0.0000795871,0.0000795451,0.0000791020,0.00007932我希望能够对文件进行排序,以便将整数值从最低到最高排序。使用 sorted 函数进行排序时,它似乎是按数字排序。a_s = sorted(List)for i in a_s: print(i)所以输出是:1,0.000081011,0.000081091,0.0000811812,0.000081222,0.0000810020,0.00007932248,0.00007982260,0.000080875,0.0000809151,0.0000791069,0.0000795871,0.000079548,0.00008092我将如何获得它以便将数值从最低值到最高值排序?
1 回答
斯蒂芬大帝
TA贡献1827条经验 获得超8个赞
尝试这个:
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.00008118
1,0.00008101
1,0.00008109
2,0.00008100
5,0.00008091
8,0.00008092
12,0.00008122
20,0.00007932
51,0.00007910
69,0.00007958
71,0.00007954
248,0.00007982
260,0.00008087
添加回答
举报
0/150
提交
取消