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

编程入门 行业动态 更新时间:2024-10-27 00:31:03
本文介绍了如何根据对象的属性对对象列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Python对象列表,我想按对象本身的属性对其进行排序.列表如下:

I've got a list of Python objects that I'd like to sort by an attribute of the objects themselves. The list looks like:

>>> ut [<Tag: 128>, <Tag: 2008>, <Tag: <>, <Tag: actionscript>, <Tag: addresses>, <Tag: aes>, <Tag: ajax> ...]

每个对象都有一个计数:

Each object has a count:

>>> ut[1].count 1L

我需要按递减计数对列表进行排序.

I need to sort the list by number of counts descending.

我已经看到了几种方法,但是我正在寻找Python的最佳实践.

I've seen several methods for this, but I'm looking for best practice in Python.

推荐答案

# 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)

有关按键排序的更多信息.

更多推荐

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

本文发布于:2023-11-30 05:48:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   属性   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!