1

可能重复:
按内部列表的最后一项对列表列表进行排序的pythonic方法

我有一个看起来像这样的数字列表:

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)
4

3 回答 3

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

或者

import operator
sorted(rows, key=operator.itemgetter(0))
于 2012-07-29T15:16:44.413 回答
1

简单地可以使用排序功能:

rows.sort()
于 2012-07-29T15:20:55.583 回答
0
from operator import itemgetter
print(sorted(rows, key=itemgetter(0)))
于 2012-07-29T15:18:49.343 回答