Sort Tuples 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 类,其中包含字段 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:排序元组 Python
基础教程推荐
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
