为什么我应该使用operator.itemgetter(x)而不是[x]?

时间:2022-09-10 11:49:06

There is a more general question here: In what situation should the built-in operator module be used in python?

这里有一个更普遍的问题:在python中,内置的操作器模块应该使用什么情况?

The top answer claims that operator.itemgetter(x) is "neater" than, presumably, than lambda a: a[x]. I feel the opposite is true.

上面的答案声称operator.itemgetter(x)比a: a[x]要“更简洁”。我觉得情况正好相反。

Are there any other benefits, like performance?

还有其他的好处吗,比如性能?

6 个解决方案

#1


18  

You shouldn't worry about performance unless your code is in a tight inner loop, and is actually a performance problem. Instead, use code that best expresses your intent. Some people like lambdas, some like itemgetter. Sometimes it's just a matter of taste.

您不应该担心性能,除非您的代码处于一个紧密的内部循环中,而且实际上是性能问题。相反,使用最能表达你意图的代码。有些人喜欢lambdas,有些人喜欢itemgetter。有时只是品味问题。

itemgetter is more powerful, for example, if you need to get a number of elements at once. For example:

例如,itemgetter更强大,如果您需要同时获得多个元素。例如:

operator.itemgetter(1,3,5)

is the same as:

是一样的:

lambda s: (s[1], s[3], s[5])

#2


11  

There are benefits in some situations, here is a good example.

在某些情况下有好处,这里有一个很好的例子。

>>> data = [('a',3),('b',2),('c',1)]
>>> from operator import itemgetter
>>> sorted(data, key=itemgetter(1))
[('c', 1), ('b', 2), ('a', 3)]

This use of itemgetter is great because it makes everything clear while also being faster as all operations are kept on the C side.

itemgetter的使用很好,因为它可以让所有的事情都变得清晰,同时也能让所有的操作都保持在C端。

>>> sorted(data, key=lambda x:x[1])
[('c', 1), ('b', 2), ('a', 3)]

Using a lambda is not as clear, it is also slower and it is preferred not to use lambda unless you have to. Eg. list comprehensions are preferred over using map with a lambda.

使用lambda并不是很清楚,它也比较慢,除非必须使用,否则最好不要使用lambda。如。列表的理解优先于使用lambda的map。

#3


9  

Performance. It can make a big difference. In the right circumstances, you can get a bunch of stuff done at the C level by using itemgetter.

的性能。它可以产生很大的不同。在适当的情况下,您可以使用itemgetter在C级获得一堆内容。

I think the claim of what is clearer really depends on which you use most often and would be very subjective

我认为更清晰的观点取决于你最常使用的是什么,而且是非常主观的。

#4


7  

Some programmers understand and use lambdas, but there is a population of programmers who perhaps didn't take computer science and aren't clear on the concept. For those programmers itemgetter() can make your intention clearer. (I don't write lambdas and any time I see one in code it takes me a little extra time to process what's going on and understand the code).

一些程序员理解并使用lambdas,但是有一群程序员可能没有使用计算机科学,也不清楚这个概念。对于那些程序员,itemgetter()可以使您的意图更加清晰。(我不写lambdas,我在代码中看到的任何时候都需要花费一点额外的时间来处理正在发生的事情并理解代码)。

If your coding for other computer science professionals go ahead and use lambdas if they are more comfortable. However, if your coding for a wider audience I suggest using itemgetter().

如果你为其他计算机科学专业人士编写代码,如果他们觉得更舒服的话,可以使用lambdas。但是,如果您的代码更广泛,我建议使用itemgetter()。

#5


1  

To add to the existing responses, itemgetter is picklable, while lambda is not. This is important if the function needs to be saved, or passed between processes (typically as part of a larger object). In the following example, replacing itemgetter with lambda will result in a PicklingError.

要添加到现有的响应中,itemgetter是可选的,而lambda则不是。如果函数需要保存,或者在进程之间传递(通常作为较大对象的一部分),这一点很重要。在下面的示例中,用lambda替换itemgetter将导致一个PicklingError错误。

from operator import itemgetter

def sort_by_key(sequence, key):
    return sorted(sequence, key=key)

if __name__ == "__main__":
    from multiprocessing import Pool

    items = [([(1,2),(4,1)], itemgetter(1)),
             ([(5,3),(2,7)], itemgetter(0))]

    with Pool(5) as p:
        result = p.starmap(sort_by_key, items)
    print(result)

#6


1  

As performance was mentioned, I've compared both methods operator.itemgetter and lambda and for a small list it turns out that operator.itemgetter outperforms lambda by 10%. I personally like the itemgetter method as I mostly use it during sort and it became like a keyword for me.

在提到性能时,我比较了两个方法操作符。itemgetter和lambda,对于一个小列表,它证明了这个操作符。itemgetter比lambda高出10%。我个人很喜欢itemgetter方法,因为我在排序过程中经常使用它,它成了我的关键字。

import operator
import timeit

x = [[12, 'tall', 'blue', 1],
[2, 'short', 'red', 9],
[4, 'tall', 'blue', 13]]


def sortOperator():
    x.sort(key=operator.itemgetter(1, 2))

def sortLambda():
    x.sort(key=lambda x:(x[1], x[2]))


if __name__ == "__main__":
    print(timeit.timeit(stmt="sortOperator()", setup="from __main__ import sortOperator", number=10**7))
    print(timeit.timeit(stmt="sortLambda()", setup="from __main__ import sortLambda", number=10**7))    

>>Tuple: 9.79s, Single: 8.835s
>>Tuple: 11.12s, Single: 9.26s

Run on Python 3.6

#1


18  

You shouldn't worry about performance unless your code is in a tight inner loop, and is actually a performance problem. Instead, use code that best expresses your intent. Some people like lambdas, some like itemgetter. Sometimes it's just a matter of taste.

您不应该担心性能,除非您的代码处于一个紧密的内部循环中,而且实际上是性能问题。相反,使用最能表达你意图的代码。有些人喜欢lambdas,有些人喜欢itemgetter。有时只是品味问题。

itemgetter is more powerful, for example, if you need to get a number of elements at once. For example:

例如,itemgetter更强大,如果您需要同时获得多个元素。例如:

operator.itemgetter(1,3,5)

is the same as:

是一样的:

lambda s: (s[1], s[3], s[5])

#2


11  

There are benefits in some situations, here is a good example.

在某些情况下有好处,这里有一个很好的例子。

>>> data = [('a',3),('b',2),('c',1)]
>>> from operator import itemgetter
>>> sorted(data, key=itemgetter(1))
[('c', 1), ('b', 2), ('a', 3)]

This use of itemgetter is great because it makes everything clear while also being faster as all operations are kept on the C side.

itemgetter的使用很好,因为它可以让所有的事情都变得清晰,同时也能让所有的操作都保持在C端。

>>> sorted(data, key=lambda x:x[1])
[('c', 1), ('b', 2), ('a', 3)]

Using a lambda is not as clear, it is also slower and it is preferred not to use lambda unless you have to. Eg. list comprehensions are preferred over using map with a lambda.

使用lambda并不是很清楚,它也比较慢,除非必须使用,否则最好不要使用lambda。如。列表的理解优先于使用lambda的map。

#3


9  

Performance. It can make a big difference. In the right circumstances, you can get a bunch of stuff done at the C level by using itemgetter.

的性能。它可以产生很大的不同。在适当的情况下,您可以使用itemgetter在C级获得一堆内容。

I think the claim of what is clearer really depends on which you use most often and would be very subjective

我认为更清晰的观点取决于你最常使用的是什么,而且是非常主观的。

#4


7  

Some programmers understand and use lambdas, but there is a population of programmers who perhaps didn't take computer science and aren't clear on the concept. For those programmers itemgetter() can make your intention clearer. (I don't write lambdas and any time I see one in code it takes me a little extra time to process what's going on and understand the code).

一些程序员理解并使用lambdas,但是有一群程序员可能没有使用计算机科学,也不清楚这个概念。对于那些程序员,itemgetter()可以使您的意图更加清晰。(我不写lambdas,我在代码中看到的任何时候都需要花费一点额外的时间来处理正在发生的事情并理解代码)。

If your coding for other computer science professionals go ahead and use lambdas if they are more comfortable. However, if your coding for a wider audience I suggest using itemgetter().

如果你为其他计算机科学专业人士编写代码,如果他们觉得更舒服的话,可以使用lambdas。但是,如果您的代码更广泛,我建议使用itemgetter()。

#5


1  

To add to the existing responses, itemgetter is picklable, while lambda is not. This is important if the function needs to be saved, or passed between processes (typically as part of a larger object). In the following example, replacing itemgetter with lambda will result in a PicklingError.

要添加到现有的响应中,itemgetter是可选的,而lambda则不是。如果函数需要保存,或者在进程之间传递(通常作为较大对象的一部分),这一点很重要。在下面的示例中,用lambda替换itemgetter将导致一个PicklingError错误。

from operator import itemgetter

def sort_by_key(sequence, key):
    return sorted(sequence, key=key)

if __name__ == "__main__":
    from multiprocessing import Pool

    items = [([(1,2),(4,1)], itemgetter(1)),
             ([(5,3),(2,7)], itemgetter(0))]

    with Pool(5) as p:
        result = p.starmap(sort_by_key, items)
    print(result)

#6


1  

As performance was mentioned, I've compared both methods operator.itemgetter and lambda and for a small list it turns out that operator.itemgetter outperforms lambda by 10%. I personally like the itemgetter method as I mostly use it during sort and it became like a keyword for me.

在提到性能时,我比较了两个方法操作符。itemgetter和lambda,对于一个小列表,它证明了这个操作符。itemgetter比lambda高出10%。我个人很喜欢itemgetter方法,因为我在排序过程中经常使用它,它成了我的关键字。

import operator
import timeit

x = [[12, 'tall', 'blue', 1],
[2, 'short', 'red', 9],
[4, 'tall', 'blue', 13]]


def sortOperator():
    x.sort(key=operator.itemgetter(1, 2))

def sortLambda():
    x.sort(key=lambda x:(x[1], x[2]))


if __name__ == "__main__":
    print(timeit.timeit(stmt="sortOperator()", setup="from __main__ import sortOperator", number=10**7))
    print(timeit.timeit(stmt="sortLambda()", setup="from __main__ import sortLambda", number=10**7))    

>>Tuple: 9.79s, Single: 8.835s
>>Tuple: 11.12s, Single: 9.26s

Run on Python 3.6