I = 2; lst = [i ** = 2而i <1000] [英] i=2; lst=[i**=2 while i&lt;1000]

查看:95
本文介绍了I = 2; lst = [i ** = 2而i <1000]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好NG,


我想知道在这个

NG中是否有提议或以前的讨论,考虑在理解列表中使用''while'' br />

#伪代码

i = 2

lst = [i ** = 2而i <1000]


当然这可以很容易地重写成

i = 2

lst = []

而i< 1000:

i ** = 2

lst.append(i)


通常我更喜欢一个衬里,比如

lines = [line.strip()for line in file(" foo")。readlines()if line]

他们使目的明确

so如果在理解列表中允许''while''这将允许

写上简洁的代码


到上面的例子,我尝试了一个解决方法

i = 2

lst = [i ** = 2 for _ in iter(lambda:_< 1000,False)]


但是由于i ** = 2

而导致SyntaxError失效,并且必须重写为

Hello NG,

I am wondering if there were proposals or previous disscussions in this
NG considering using ''while'' in comprehension lists

# pseudo code
i=2
lst=[i**=2 while i<1000]

of course this could be easily rewritten into
i=2
lst=[]
while i<1000:
i**=2
lst.append(i)

usually I would prefer one liners like
lines=[line.strip() for line in file("foo").readlines() if line]
they make the purpose clear
so if ''while'' were allowed in comprehension list this would allow
to write concise code

to the example above, I tried a workaround
i=2
lst=[i**=2 for _ in iter(lambda:_<1000, False)]

but this failes with SyntaxError because of i**=2
and must be also rewritten into

i = 2
def f():
.... global i

....我** = 2

....返回我

.... lst = [f()for _ in iter(lambda: i< 1000,False)]
lst
i=2
def f(): .... global i
.... i**=2
.... return i
.... lst=[f() for _ in iter(lambda:i<1000, False)]
lst



[4,16,256,65536]


我认为这与此相比损失了

i = 2

lst = [i ** = 2而i <1000]


问候,Daniel


[4, 16, 256, 65536]

I think this loses compared with
i=2
lst=[i**=2 while i<1000]

Regards, Daniel

推荐答案

DanielSchüle写道:
Daniel Schüle wrote:
Hello NG,

我想知道在这个
NG考虑在理解列表中使用''while'时是否有提议或先前的讨论

#pseudo code
i = 2
lst = [i ** = 2而我<1000]

当然这可以很容易地改写成
我= 2
lst = []
而我< 1000:
我** = 2
lst.append(i)
Hello NG,

I am wondering if there were proposals or previous disscussions in this
NG considering using ''while'' in comprehension lists

# pseudo code
i=2
lst=[i**=2 while i<1000]

of course this could be easily rewritten into
i=2
lst=[]
while i<1000:
i**=2
lst.append(i)




因为你没有增加我,所以无休止地循环。

你可以在范围(1000)中使用i ** = 2代替



That would loop endlessly since you don''t increment i.
You can use i**=2 for i in range(1000) instead




DH写道:

D H wrote:
DanielSchüle写道:
Daniel Schüle wrote:
Hello NG,

我想知道是否有提案或以前在这个NG中的讨论考虑在理解列表中使用''while''

#伪代码
i = 2
lst = [i ** = 2 while我< 1000]

当然这可以很容易地改写成
我= 1000:
我* * = 2
lst.append(i)
Hello NG,

I am wondering if there were proposals or previous disscussions in this
NG considering using ''while'' in comprehension lists

# pseudo code
i=2
lst=[i**=2 while i<1000]

of course this could be easily rewritten into
i=2
lst=[]
while i<1000:
i**=2
lst.append(i)



这会无休止地循环,因为你不会增加我。
你可以使用i ** = 2我在范围(1000)而不是



That would loop endlessly since you don''t increment i.
You can use i**=2 for i in range(1000) instead




我不认为可以在列表理解或生成器中使用赋值

表达式。限制非常像lambda。



I don''t think one can use assignment in list comprehension or generator
expression. The limitation is very much like lambda.


bo **** @ gmail.com 写道:
你可以在范围(1000)中使用i ** = 2代替
You can use i**=2 for i in range(1000) instead



我不认为可以在列表理解或生成器
表达式中使用赋值。限制非常像lambda。


I don''t think one can use assignment in list comprehension or generator
expression. The limitation is very much like lambda.




i ** 2



i**2


这篇关于I = 2; lst = [i ** = 2而i <1000]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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