排序元组Python [英] Sort Tuples Python

查看:86
本文介绍了排序元组Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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?

我认为也许更好的实现是使用字段namescore

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆