列表理解何时不合适? [英] When is List Comprehension inappropriate?

查看:80
本文介绍了列表理解何时不合适?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近了解了列表理解是如何工作的,并且我发现它非常酷。
非常酷。然而,我很担心,我可能会把它塞进它不属于它的地方。


什么是最好的pythony"这样做的方法:


even = []

for x in range(0,width,2):

for y在范围内(0,高度,2):

color = im.getpixel((x,y))

even.append(((x,y),颜色))


与列表理解:


even2 = [((x,y),im.getpixel((x,y) ))对于范围(0,宽度,2)中的x,对于y

的范围(0,高度,2)]


是否存在计算差异创建一个空白列表和

附加到它而不是列表理解?除了简短漂亮的代码之外还有它的好处吗?


请随意告诉我一个不同的方法来做到这一点。


谢谢,

Ben

I have recently learned how list comprehension works and am finding it
extremely cool. I am worried, however, that I may be stuffing it into
places that it does not belong.

What''s the most "pythony" way to do this:

even = []
for x in range(0,width,2):
for y in range(0,height,2):
color = im.getpixel((x,y))
even.append(((x,y), color))

versus list comprehension:

even2 = [((x,y), im.getpixel((x,y))) for x in range(0,width,2) for y
in range(0,height,2)]

Is there a computational difference in creating a blank list and
appending to it versus doing a list comprehension? Are there
advantages to it outside of short and pretty code?

Feel free to tell me a different way to do this, as well.

Thanks,
Ben

推荐答案

Ben< be * *******@gmail.com写道:
Ben <be********@gmail.comwrote:

我最近学会了列表理解是如何工作的并且找到它

非常酷。然而,我很担心,我可能会把它塞进它不属于它的地方。


什么是最好的pythony"这样做的方法:


even = []

for x in range(0,width,2):

for y在范围内(0,高度,2):

color = im.getpixel((x,y))

even.append(((x,y),颜色))


与列表理解:


even2 = [((x,y),im.getpixel((x,y) ))对于范围(0,宽度,2)中的x,对于y

的范围(0,高度,2)]


是否存在计算差异创建一个空白列表和

附加到它而不是列表理解?除了简短漂亮的代码之外,它有什么好处吗?


随意告诉我一个不同的方法来做到这一点。
I have recently learned how list comprehension works and am finding it
extremely cool. I am worried, however, that I may be stuffing it into
places that it does not belong.

What''s the most "pythony" way to do this:

even = []
for x in range(0,width,2):
for y in range(0,height,2):
color = im.getpixel((x,y))
even.append(((x,y), color))

versus list comprehension:

even2 = [((x,y), im.getpixel((x,y))) for x in range(0,width,2) for y
in range(0,height,2)]

Is there a computational difference in creating a blank list and
appending to it versus doing a list comprehension? Are there
advantages to it outside of short and pretty code?

Feel free to tell me a different way to do this, as well.



当我正在构建一个列表时,我喜欢列表推导,最初是
为空,在for循环中使用.append调用(可能除非我需要(例如)有条件的休息,LC不支持。


IOW,我会使用一个if if guard)你的例子中的LC。但是,我会把它格式化得更好

整齐地说:


even2 = [((x,y),im.getpixel((x,y)))

for x in range(0,width,2)

for y in range(0,height,2)]


虽然我猜这是一个品味问题。


有些人认为不应该使用LC,除非是非常简单的案件,但我个人不同意这种立场。


不应使用LC的情况是那些你不是真正建立如上所述列表的b $ b例如只是循环。


要检查LC是否比更多

扩展循环更快,更慢或等于,请使用timeit,例如:


脑:〜/ py25 / Doc alex

I like list comprehensions when I''m building up a list, originally
empty, with .append calls within for loops (possibly with an if guard),
unless I need (e.g.) a conditional break, which LCs don''t support.

IOW, I would use a LC in your example. However, I would format it more
neatly:

even2 = [((x,y), im.getpixel((x,y)))
for x in range(0,width,2)
for y in range(0,height,2)]

though I guess that''s a matter of taste.

Some people think that LCs should not be used except for extremely
simple cases, but I personally disagree with that stance.

The cases where an LC should NOT be used are those in which you are not
really building a list as above described, but e.g. "just looping".

To check whether LC is faster than, slower than, or equal to a more
extensive loop, use timeit, e.g.:

brain:~/py25/Doc alex


python -mtimeit -s''xs = range(83)'''L = []'''for x in

xs:L.append(x * x)''

10000循环,最佳3:34.6 usec每循环


脑:〜/ py25 / Doc alex
python -mtimeit -s''xs=range(83)'' ''L=[]'' ''for x in
xs: L.append(x*x)''
10000 loops, best of 3: 34.6 usec per loop

brain:~/py25/Doc alex


python -mtimeit -s''xs = range(83)''''L = [x * x for x in

xs]''

100000循环,最佳3:19.4 usec每循环


所以对于这个简单的例子,它可能看起来像LC更快;

但是:


脑:〜/ py25 / Doc alex
python -mtimeit -s''xs=range(83)'' ''L=[x*x for x in
xs]''
100000 loops, best of 3: 19.4 usec per loop

So for this simple case, it may look like the LC is much faster;
however:

brain:~/py25/Doc alex


这篇关于列表理解何时不合适?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆