> python2.4生成器表达式> python2.3列表表达式 [英] python2.4 generator expression > python2.3 list expression
问题描述
我需要将生成器表达式转换为列表表达式,这样它就可以在python 2.3下运行。
我重写了这个:
范围内的c(128):
even_odd =(sum(bool(c& 1<< b),范围(8)中的b))& ; 1
如下:
范围内的c(128):
bo = [bool(c) & 1<< b)b范围(8)]
even_odd = sum(bo)& 1
似乎工作,有没有更好的方法呢?
snacktime写道:我需要将生成器表达式转换为列表表达式,以便它可以在python 2.3下工作。
我重写了这个:
范围(128):
even_odd =(sum(bool(c& 1<< b),范围(8)中的b))& 1
如下:
对于范围内的c(128):
bo = [bool(c& 1<< b),范围为b (8)]
even_odd = sum(bo)& 1
好像有用,有没有更好的方法呢?
好吧,如果你对你的发电机表达感到满意,那么你可以使用
几乎完全相同的语法:
范围内的c(128):
even_odd =(sum([ bool(c& 1<< b)对于范围(8)中的b]))& 1
不需要''bo''变量...
STeVe
>
snacktime写道:我需要将生成器表达式转换为列表表达式,以便它可以在python 2.3下工作。
我重写了这个:
对于范围(128)中的c:
even_odd =(sum(bool(c& 1<< b),范围(8)中的b))& 1
如下:
对于范围内的c(128):
bo = [bool(c& 1<< b),范围为b (8)]
even_odd = sum(bo)& 1
好像有用,有没有更好的方法呢?
如果你想保留它作为一个没有的发电机在内存中建立一个列表
,你可以使用itertools:
import itertools
for c in范围(128):
def _even_odd_func(b):返回bool(c& 1<< b)
even_odd =(sum(itertools.imap(_even_odd_func, xrange(8))))& 1
你使用range()而不是xrange()的事实表明
你可能不关心这个。 ;-)
-
Michael Hoffman
snacktime写道:
我需要将生成器表达式转换为列表表达式,以便它可以在python 2.3下工作。
我重写了这个:
for c in range (128):
even_odd =(sum(bool(c& 1<< b),范围(8)中的b))& 1
如下:
对于范围内的c(128):
bo = [bool(c& 1<< b),范围为b (8)]
even_odd = sum(bo)& 1
好像有用,有没有更好的方法呢?
对零进行求和似乎毫无意义,所以
< blockquote class =post_quotes>for c in range(128):
.... print len(如果c& 1< b>那么,[范围(8)中的b为1)& 1,
....
0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1
1 0 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0
1 0 0 1 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0
0 1 0 1 1 0 0 1 1 0 1 0 0 1
同样的简化适用于genexps,但你必须使用sum( )那里是
而不是len()。另一个优化是预先计算
位掩码[1<<循环外的b为范围(8)中的b。
彼得
I need to convert a generator expression to a list expression so it
will work under python 2.3.
I rewrote this:
for c in range(128):
even_odd = (sum(bool(c & 1<<b) for b in range(8))) & 1
As this:
for c in range(128):
bo = [bool(c & 1<<b) for b in range(8)]
even_odd = sum(bo) & 1
Seems to work, is there a better way to do this?
snacktime wrote:I need to convert a generator expression to a list expression so it
will work under python 2.3.
I rewrote this:
for c in range(128):
even_odd = (sum(bool(c & 1<<b) for b in range(8))) & 1
As this:
for c in range(128):
bo = [bool(c & 1<<b) for b in range(8)]
even_odd = sum(bo) & 1
Seems to work, is there a better way to do this?
Well, if you were happy with your generator expression, you can use
almost exactly the same syntax:
for c in range(128):
even_odd = (sum([bool(c & 1<<b) for b in range(8)])) & 1
No need for the ''bo'' variable...
STeVe
snacktime wrote:I need to convert a generator expression to a list expression so it
will work under python 2.3.
I rewrote this:
for c in range(128):
even_odd = (sum(bool(c & 1<<b) for b in range(8))) & 1
As this:
for c in range(128):
bo = [bool(c & 1<<b) for b in range(8)]
even_odd = sum(bo) & 1
Seems to work, is there a better way to do this?
If you want to keep it as a generator that doesn''t build a list
in memory, you can use itertools:
import itertools
for c in range(128):
def _even_odd_func(b): return bool(c & 1<<b)
even_odd = (sum(itertools.imap(_even_odd_func, xrange(8)))) & 1
The fact that you used range() instead of xrange() indicates that
you may not care about this, though. ;-)
--
Michael Hoffman
snacktime wrote:
I need to convert a generator expression to a list expression so it
will work under python 2.3.
I rewrote this:
for c in range(128):
even_odd = (sum(bool(c & 1<<b) for b in range(8))) & 1
As this:
for c in range(128):
bo = [bool(c & 1<<b) for b in range(8)]
even_odd = sum(bo) & 1
Seems to work, is there a better way to do this?
Summing over zeros seems pointless, so
for c in range(128):
.... print len([1 for b in range(8) if c & 1 << b]) & 1,
....
0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1
1 0 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0
1 0 0 1 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0
0 1 0 1 1 0 0 1 1 0 1 0 0 1
The same simplification works for genexps, but you have to use sum() there
instead of len(). Another optimization would be to precalculate the
bitmasks [1 << b for b in range(8)] outside the loop.
Peter
这篇关于> python2.4生成器表达式> python2.3列表表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!