使用list.sort函数
def take_second(elem): num = float(elem[-2]) return num list_info = [["sd452f", 3, "lsd3455543"], ["425f", 9, "l"], ["93345df", 2, "l345fj"], ["sd4535", 8, "ls3453j"], ["sdf", 0, "ls53733"]] list_info.sort(reverse=True, key=take_second) print(list_info)
对子列表进行排序,参考子列表的第二项元素,reverse=True 表示倒序排序
输出结果为:
[['425f', 9, 'l'], ['sd4535', 8, 'ls3453j'], ['sd452f', 3, 'lsd3455543'], ['93345df', 2, 'l345fj'], ['sdf', 0, 'ls53733']]