Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

python - 列表理解与lambda +过滤器(list comprehension vs. lambda + filter)

I happened to find myself having a basic filtering need: I have a list and I have to filter it by an attribute of the items.

(我碰巧发现自己有一个基本的过滤需求:我有一个列表,并且必须按项目的属性对其进行过滤。)

My code looked like this:

(我的代码如下所示:)

my_list = [x for x in my_list if x.attribute == value]

But then I thought, wouldn't it be better to write it like this?

(但是后来我想,这样写会更好吗?)

my_list = filter(lambda x: x.attribute == value, my_list)

It's more readable, and if needed for performance the lambda could be taken out to gain something.

(它更具可读性,并且如果需要性能,则可以取出lambda以获得某些东西。)

Question is: are there any caveats in using the second way?

(问题是:使用第二种方法是否有警告?)

Any performance difference?

(有任何性能差异吗?)

Am I missing the Pythonic Way? entirely and should do it in yet another way (such as using itemgetter instead of the lambda)?

(我是否完全想念Pythonic Way?,应该以另一种方式来做到这一点(例如,使用itemgetter而不是lambda)吗?)

  ask by Agos translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is strange how much beauty varies for different people.

(奇怪的是,不同的人有多少美丽。)

I find the list comprehension much clearer than filter + lambda , but use whichever you find easier.

(我发现列表理解比filter + lambda更清晰,但请使用任何您发现更容易的方法。)

There are two things that may slow down your use of filter .

(有两件事可能会减慢您使用filter 。)

The first is the function call overhead: as soon as you use a Python function (whether created by def or lambda ) it is likely that filter will be slower than the list comprehension.

(第一个是函数调用开销:使用Python函数(无论是由def还是lambda创建)后,filter的运行速度可能会比list理解的速度慢。)

It almost certainly is not enough to matter, and you shouldn't think much about performance until you've timed your code and found it to be a bottleneck, but the difference will be there.

(几乎可以肯定,这还不够重要,并且在对代码进行计时并发现它是瓶颈之前,您不应该对性能进行太多的考虑,但是区别仍然存在。)

The other overhead that might apply is that the lambda is being forced to access a scoped variable ( value ).

(可能适用的其他开销是,lambda被强制访问作用域变量( value )。)

That is slower than accessing a local variable and in Python 2.x the list comprehension only accesses local variables.

(这比访问局部变量要慢,并且在Python 2.x中,列表推导仅访问局部变量。)

If you are using Python 3.x the list comprehension runs in a separate function so it will also be accessing value through a closure and this difference won't apply.

(如果您使用的是Python 3.x,则列表推导是在单独的函数中运行的,因此它也将通过闭包来访问value ,并且这种区别将不适用。)

The other option to consider is to use a generator instead of a list comprehension:

(要考虑的另一个选项是使用生成器而不是列表推导:)

def filterbyvalue(seq, value):
   for el in seq:
       if el.attribute==value: yield el

Then in your main code (which is where readability really matters) you've replaced both list comprehension and filter with a hopefully meaningful function name.

(然后,在您的主要代码(这才是真正的可读性)中,您已经用有希望的有意义的函数名称替换了列表理解和过滤器。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...