如何根據對象的屬性對對象列表進行排序? - How to sort a list of objects based on an attribute of the objects?

問題:

I've got a list of Python objects that I'd like to sort by an attribute of the objects themselves. 我有一個Python對象列表,我想按對象自己的屬性對其進行排序。 The list looks like: 該列表以下所示: ajax

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

Each object has a count: 每一個對象都有一個計數: this

>>> ut[1].count
1L

I need to sort the list by number of counts descending. 我須要按遞減計數對列表進行排序。 spa

I've seen several methods for this, but I'm looking for best practice in Python. 我已經看到了幾種方法,可是我正在尋找Python的最佳實踐。 .net


解決方案:

參考一: https://stackoom.com/question/1gwn/如何根據對象的屬性對對象列表進行排序
參考二: https://oldbug.net/q/1gwn/How-to-sort-a-list-of-objects-based-on-an-attribute-of-the-objects
相關文章
相關標籤/搜索