本文介绍了列表理解与lambda +过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的代码看起来像是这样:这:

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


$ b

然后我想,这样写是不是更好?

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

它更具可读性,如果性能需要,lambda可以被取出来获得某些东西。

问题是:在使用第二种方法时是否有任何注意事项?任何性能差异?我完全错过了Pythonic Way™,应该用另一种方式来完成它(例如使用itemgetter而不是lambda)吗? 解决方案

对于不同的人来说,有多少美女变化很奇怪。我发现列表理解比 filter + lambda 要清楚得多,不过使用哪一个更容易。但是,请停止提供已经用于内置插件的变量名称,这很让人困惑。



有两件事可能会降低您对过滤器



第一个是函数调用开销:只要您使用Python函数(无论是由 def lambda ),过滤器很可能会比列表理解慢。这几乎肯定是不够的,你不应该考虑性能,直到你定时代码,并发现它是一个瓶颈,但差异将在那里。



另一个可能适用的开销是lambda被强制访问一个作用域变量( value )。这比访问本地变量要慢,在Python 2.x中,列表理解只能访问局部变量。如果您使用的是Python 3.x,则列表理解将在单独的函数中运行,因此它也将通过闭包访问 value ,并且此差异不适用。



另一种选择是使用生成器而不是列表理解:

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

然后在你的主代码中(这是可读性真正重要的地方),你用一个有希望的函数名称取代了列表理解和过滤器。


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.

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

解决方案

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. However, do stop giving your variables names already used for built-ins, that's confusing.

There are two things that may slow down your use of 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. 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). That is slower than accessing a local variable and in Python 2.x the list comprehension only accesses local variables. 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.

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.

这篇关于列表理解与lambda +过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:44