python - 如何根据对象的属性对对象列表进行排序?

标签 python list sorting count

我有一个 Python 对象列表,我想按每个对象的特定属性对其进行排序:

>>> ut
[Tag(name="toe", count=10), Tag(name="leg", count=2), ...]

如何按 .count 对列表进行降序排序?

最佳答案

# To sort the list in place...
ut.sort(key=lambda x: x.count, reverse=True)

# To return a new list, use the sorted() built-in function...
newlist = sorted(ut, key=lambda x: x.count, reverse=True)

更多关于 sorting by keys .

关于python - 如何根据对象的属性对对象列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/403421/

相关文章:

python - Pika/RabbitMQ 连接问题 - 运行 VMWare CentOS 6.3

C# List<string> OrderBy 拆分

java - 反转列表中元素的顺序

java - 从 Map<String,String> 获取 3 个最高值

Python 按另一个位置数组排序数组

c - 在 C 中按受欢迎程度对字符进行排序

python - pymssql:在非 Windows 机器上运行时如何使用 Windows 身份验证

python - 检索 DataFrame 的整行

python - 使用变量 "interpolation"创建导入语句

python - 生成器因 NoneType 错误而失败