I'd like to use the somelist.sort() method to do this if possible.
如果可能的话,我想使用somelist.sort()方法来执行此操作。
I have a list containing objects, all objects have a member variable resultType that is an integer. I'd like to sort the list using this number.
我有一个包含对象的列表,所有对象都有一个成员变量resultType,它是一个整数。我想用这个数字对列表进行排序。
How do I do this?
我该怎么做呢?
Thanks!
谢谢!
3 个解决方案
#1
67
somelist.sort(key = lambda x: x.resultType)
Here's another way to do the same thing that you will often see used:
这是另一种做同样事情的方法,你经常会看到:
import operator
s.sort(key = operator.attrgetter('resultType'))
You might also want to look at sorted
if you haven't seen it already. It doesn't modify the original list - it returns a new sorted list.
如果您还没有看过它,您可能还想查看已排序。它不会修改原始列表 - 它会返回一个新的排序列表。
#2
10
Of course, it doesn't have to be a lambda. Any function passed in, such as the below one, will work
当然,它不一定是一个lambda。传入的任何函数(例如下面的函数)都可以使用
def numeric_compare(x, y):
if x > y:
return 1
elif x == y:
return 0
else: #x < y
return -1
a = [5, 2, 3, 1, 4]
a.sort(numeric_compare)
Source : Python Sorting
来源:Python排序
So, in your case ...
所以,在你的情况下......
def object_compare(x, y):
if x.resultType > y.resultType:
return 1
elif x.resultType == y.resultType:
return 0
else: #x.resultType < y.resultType
return -1
a.sort(object_compare)
The aforementioned lambda is definitely the most compact way of doing it, but there's also using operator.itemgetter.
前面提到的lambda绝对是最紧凑的方式,但也有使用operator.itemgetter。
import operator
#L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
map(operator.itemgetter(0), L)
#['c', 'd', 'a', 'b']
map(operator.itemgetter(1), L)
#[2, 1, 4, 3]
sorted(L, key=operator.itemgetter(1))
#[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
So you'd use itemgetter('resultType'). (Assuming getitem is defined.)
所以你要使用itemgetter('resultType')。 (假设定义了getitem。)
sorted(L, key=operator.itemgetter('resultType'))
#3
1
somelist.sort(cmp = lambda x, y: cmp(x.resultType, y.resultType))
Is better than:
比以下更好:
somelist.sort(key = lambda x: x.resultType)
In the first case we pass in a comparison function which is used to pair-wise compare the elements in the list. In the second case we allocate a new list of pairs of the result of the key function, and the original value. Then we sort that list, then strip off the key values from the pairs. This is really useful if your comparison function is expensive, but is just a waste of memory if the comparison is really cheap.
在第一种情况下,我们传入一个比较函数,用于成对地比较列表中的元素。在第二种情况下,我们分配一个新的键函数结果列表和原始值。然后我们对该列表进行排序,然后从成对中删除键值。如果您的比较功能很昂贵,这非常有用,但如果比较非常便宜,那只会浪费内存。
That is, the expansion of the key version looks something like this:
也就是说,密钥版本的扩展看起来像这样:
l = [y for x,y in sorted(zip([key(i) for i in l], l))]
For a simple key function, that is clearly too much overhead, so instead I would suggest using the lighter function based sort.
对于一个简单的键功能,这显然是太多开销,所以我建议使用基于较轻功能的排序。
Note that the cmp function parameter needs to return -1, 0, 1 in the less than, equal and greater than cases. You could write that yourself, but you can also use the built in cmp function which is clearer.
请注意,cmp函数参数需要在小于,等于和大于的情况下返回-1,0,1。你可以自己编写,但你也可以使用内置的cmp函数,它更清晰。
#1
67
somelist.sort(key = lambda x: x.resultType)
Here's another way to do the same thing that you will often see used:
这是另一种做同样事情的方法,你经常会看到:
import operator
s.sort(key = operator.attrgetter('resultType'))
You might also want to look at sorted
if you haven't seen it already. It doesn't modify the original list - it returns a new sorted list.
如果您还没有看过它,您可能还想查看已排序。它不会修改原始列表 - 它会返回一个新的排序列表。
#2
10
Of course, it doesn't have to be a lambda. Any function passed in, such as the below one, will work
当然,它不一定是一个lambda。传入的任何函数(例如下面的函数)都可以使用
def numeric_compare(x, y):
if x > y:
return 1
elif x == y:
return 0
else: #x < y
return -1
a = [5, 2, 3, 1, 4]
a.sort(numeric_compare)
Source : Python Sorting
来源:Python排序
So, in your case ...
所以,在你的情况下......
def object_compare(x, y):
if x.resultType > y.resultType:
return 1
elif x.resultType == y.resultType:
return 0
else: #x.resultType < y.resultType
return -1
a.sort(object_compare)
The aforementioned lambda is definitely the most compact way of doing it, but there's also using operator.itemgetter.
前面提到的lambda绝对是最紧凑的方式,但也有使用operator.itemgetter。
import operator
#L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
map(operator.itemgetter(0), L)
#['c', 'd', 'a', 'b']
map(operator.itemgetter(1), L)
#[2, 1, 4, 3]
sorted(L, key=operator.itemgetter(1))
#[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
So you'd use itemgetter('resultType'). (Assuming getitem is defined.)
所以你要使用itemgetter('resultType')。 (假设定义了getitem。)
sorted(L, key=operator.itemgetter('resultType'))
#3
1
somelist.sort(cmp = lambda x, y: cmp(x.resultType, y.resultType))
Is better than:
比以下更好:
somelist.sort(key = lambda x: x.resultType)
In the first case we pass in a comparison function which is used to pair-wise compare the elements in the list. In the second case we allocate a new list of pairs of the result of the key function, and the original value. Then we sort that list, then strip off the key values from the pairs. This is really useful if your comparison function is expensive, but is just a waste of memory if the comparison is really cheap.
在第一种情况下,我们传入一个比较函数,用于成对地比较列表中的元素。在第二种情况下,我们分配一个新的键函数结果列表和原始值。然后我们对该列表进行排序,然后从成对中删除键值。如果您的比较功能很昂贵,这非常有用,但如果比较非常便宜,那只会浪费内存。
That is, the expansion of the key version looks something like this:
也就是说,密钥版本的扩展看起来像这样:
l = [y for x,y in sorted(zip([key(i) for i in l], l))]
For a simple key function, that is clearly too much overhead, so instead I would suggest using the lighter function based sort.
对于一个简单的键功能,这显然是太多开销,所以我建议使用基于较轻功能的排序。
Note that the cmp function parameter needs to return -1, 0, 1 in the less than, equal and greater than cases. You could write that yourself, but you can also use the built in cmp function which is clearer.
请注意,cmp函数参数需要在小于,等于和大于的情况下返回-1,0,1。你可以自己编写,但你也可以使用内置的cmp函数,它更清晰。