gpt4 book ai didi

python - 自定义按升序对数字列表进行排序

转载 作者:行者123 更新时间:2023-12-01 05:57:48 25 4
gpt4 key购买 nike

Possible Duplicate:
pythonic way to sort a list of lists by the last item of the inner list

我有一个数字列表,如下所示:

rows = [[100,1.17902838317,4.30281847041,4.30598958666,4.31258472331,10.9269044537,1.17902838317]
[110,1.3978813526,1.39847341928,1.39950407438,1.4016475818,5.17927032236,1.3978813526]
[120,2.95045527752,2.95170493026,2.95388029498,2.95840451497,7.49578397746,2.95045527752]
[30,3.14439495993,3.145726755,3.14714447001,3.15196469714,7.9862122826,3.14439495993]
[40,3.35171204965,3.62204712952,3.62308999951,3.62767337976,8.49979359433,3.62120669609]
[50,2.63010055396,2.66403524005,2.66637361988,2.67045748911,6.76620535091,2.63010055396]
[60,3.09088634745,3.09219547918,3.09447438343,3.09921394005,9.71470661115,3.09088634745]
[70,1.42116996887,1.42199838776,1.42411423836,2.27074446576,3.60990507963,2.26985641041]
[80,1.7602934006,1.76103896537,1.76233682613,1.76503605518,4.4721162759,1.7602934006]
[90,1.27784662871,1.2783878552,1.27933000894,1.28128945544,3.24643534107,1.27784662871]
]

我想编写一个排序函数,它将返回使用第一列中的整数排序的原始列表,因此排序后的列表将如下所示:

30,3.14439495993,3.145726755,3.14714447001,3.15196469714,7.9862122826,3.14439495993
40,3.35171204965,3.62204712952,3.62308999951,3.62767337976,8.49979359433,3.62120669609
50,2.63010055396,2.66403524005,2.66637361988,2.67045748911,6.76620535091,2.63010055396
60,3.09088634745,3.09219547918,3.09447438343,3.09921394005,9.71470661115,3.09088634745
70,1.42116996887,1.42199838776,1.42411423836,2.27074446576,3.60990507963,2.26985641041
80,1.7602934006,1.76103896537,1.76233682613,1.76503605518,4.4721162759,1.7602934006
90,1.27784662871,1.2783878552,1.27933000894,1.28128945544,3.24643534107,1.27784662871
100,1.17902838317,4.30281847041,4.30598958666,4.31258472331,10.9269044537,1.17902838317
110,1.3978813526,1.39847341928,1.39950407438,1.4016475818,5.17927032236,1.3978813526
120,2.95045527752,2.95170493026,2.95388029498,2.95840451497,7.49578397746,2.95045527752

如何编写这样的自定义排序函数?

到目前为止的尝试:

def mysort_func(row):
# get the integer of row[1]
# do something with it (simply return it?)
pass


new_list = sorted(rows, key=mysort_func)

最佳答案

sorted(rows, key=lambda x: x[0])

import operator
sorted(rows, key=operator.itemgetter(0))

关于python - 自定义按升序对数字列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11710345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com