问题描述
我的 Blender python 代码中有一个元组列表
I have a list of tuples in my Blender python code
scores=[(1489,"Sean"), (2850,"Bob"), (276,"Crap Player"), (78495, "Great Player"), (8473, "Damian"), (4860, "Andy"), (0, "Stephen")]
我正在尝试使用它按分数对它们进行排序
I'm trying to sort them by their score by using this
sorted(scores, key=lambda score: score[0], reverse=True)
但这不起作用.我不知道为什么.有什么建议吗?
but this is not working. I have no idea why. Any tips?
我考虑过也许更好的实现是创建一个新的 Score
类,其中包含字段 name
和 score
I've considered maybe a better implementation is to create a new Score
class with fields name
and score
感谢大家的快速回复
sorted
方法没有给我任何错误,但没有排序.我使用了 sort()
并且它有效.
it was giving me no errors with the sorted
method but was not sorting.
I used the sort()
and it works.
我认为 python 在 Blender 中可能有点奇怪?
I think python is just a little weird in Blender maybe?
谢谢!
推荐答案
随便做吧:
print sorted(scores, reverse=True)
[(78495, 'Great Player'), (8473, 'Damian'), (4860, 'Andy'), (2850, 'Bob'), (1489, 'Sean'), (276, 'Crap Player'), (0, 'Stephen')]
如果你想就地排序,你可以使用 scores.sort(reverse=True)
,顺便说一下,在元组列表的情况下,排序函数默认按第一项排序,第二个项目..
you can use scores.sort(reverse=True)
if you want to sort in place, and by the way the sort function in case of list of tuple by default sort by first item , second item ..
这篇关于排序元组 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!