排序元组 Python

Sort Tuples Python(排序元组 Python)
本文介绍了排序元组 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?

我考虑过也许更好的实现是创建一个新的 Score 类,其中包含字段 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)