如果您想在排序前更改第三个元素的类型,那么您可以使用列表理解,然后进行排序。
list_of_lists[:] = [sublist[:2] + [float(sublist[2])] for sublist in list_of_lists]
list_of_lists.sort(key=lambda x:x[2])
print(list_of_lists)
输出:
[['2', 'Sex & The City', 4.732],
['10', 'Book of Bobba Fett', 32.245],
['132', 'And Just Like That', 77.014],
['43', 'Yellowjackets', 98.6]]
但如果你只想
sort
方法将第三个元素视为浮点,然后可以使用
float
lambda中的函数:
list_of_lists.sort(key=lambda x: float(x[2]))
print(list_of_lists)
输出:
[['2', 'Sex & The City', '4.732'],
['10', 'Book of Bobba Fett', '32.245'],
['132', 'And Just Like That', '77.014'],
['43', 'Yellowjackets', '98.6']]