在python中排序列表列表时输出错误

时间:2017-11-21 21:08:29

标签: python list sorting

我有这个列表的示例列表:

[[0.7578684, 'Yes, import function which lets you load items'],[1.7032554, 'Use the Test tools to edit'], [0.58279467, 'Yes, use the Designer UI Import feature to restore content from the JSON file.']]

我想根据每个list中的descending order值对integer中的list进行排序。我写了这些行,但输出错误:

sorted_answer = answer.sort(key=lambda x: float(x[0]),reverse = True)
print(sorted_answer)

当我执行此操作时,我得到None作为sorted_answer列表的输出。我犯的错是什么?

2 个答案:

答案 0 :(得分:1)

你到位排序列表,排序返回无。如果你想要排序返回功能,请使用已排序的

answer.sort(key=lambda x: float(x[0]),reverse = True)
print(answer)

---更新---
我不知道。为什么人们投票。 answer.sort(key=lambda x: float(x[0]),reverse = True)返回None并返回po assign回答,因此答案为None。排序功能到位,无需分配回来

答案 1 :(得分:1)

sorted(answer, reverse=True)

给出

[[1.7032554, 'Use the Test tools to edit'], 
[0.7578684, 'Yes, import function which lets you load items'], 
[0.58279467, 'Yes, use the Designer UI Import feature to restore content from the JSON file.']]