我有一个 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/