"对于"循环和列表理解相似性 [英] "For" loop and list comprehension similarity
问题描述
大家好,
如果以前提出这件事,我道歉,我找不到任何之前的
艺术品。 :-)。
不止一次,我发现自己想要使用条件
循环。像这样(当然有无效语法错误):
for c in c if< test> ;:
print i * 2
....因为它类似于列表理解构造:
[i * 2 for i in c if<测试>]
---------
这是构造中的预期差异吗?可用的等价物
感觉有点尴尬:
$ c $ b for c in c:
if< test>:
打印i * 2
好奇。谢谢!
谢尔盖。
s。********* @ gmail.com 写道:
更多不止一次,我发现自己想要使用条件回路。像这样(当然有无效语法错误):
我在c中如果< test>:
打印i * 2
也许有一个PEP,真的不知道...
目前,唯一合理的选择是你在下面写的:
可用的等价物感觉有点尴尬:
我在c:
如果< test>:
打印i * 2
这确实看起来不太好,特别是如果你有很多代码,而不仅仅是
打印。另一种避免双重缩进的替代方案是:* b / b
,如果不是< test>:继续
* 2
感谢您回复,Mitja! *是*一个不错的选择。
你认为在comp.python.devel上问他们是否会感兴趣
感兴趣在PEP关于这个(如果没有)?
干杯,
谢尔盖。
在2006-03-26, s.*********@gmail。 com < s。********* @ gmail.com>写道:大家好,
我很抱歉,如果之前有这个,我找不到任何之前的艺术。 :-)。
在不止一次的情况下,我发现自己想要使用条件
循环。像这样(当然错误的语法错误):
我在c中如果< test>:
打印i * 2
。因为它类似于列表理解构造:
[i * 2 for c in c< test>]
---------
这是构造中的预期差异吗?可用的等价物感觉有点尴尬:
我在c:
如果< test>:
打印i * 2
for j in [i * 2 for i in c in ct< test>]:
print j
- -
格兰特爱德华兹格兰特哇! ..我想知道我是否应该告诉他们我的
visi.com以前的生活是完整的
STRANGER?
Hi All,
I apologize if this was brought up before, I couldn''t find any "prior
art" :-).
On more than one occasion, I found myself wanting to use a "conditional
loop" like this (with "Invalid syntax" error, of course):
for i in c if <test>:
print i*2
....because it''s similar to the list comprehension construct:
[i*2 for i in c if <test>]
---------
Is this the intended difference in constructs? The available equivalent
feels a bit awkward:
for i in c:
if <test>:
print i*2
Just curious. Thanks!
Sergey.
s.*********@gmail.com wrote:
On more than one occasion, I found myself wanting to use a "conditional
loop" like this (with "Invalid syntax" error, of course):
for i in c if <test>:
print i*2
Maybe there''s been a PEP, don''t really know...
Currently, the only sensible alternative is what you''ve written below:
The available equivalent
feels a bit awkward:
for i in c:
if <test>:
print i*2
This indeed doesn''t look nice, especially if you''ve got lots of code instead of just
print. An alternative which avoids double indentation is
for i in c:
if not <test>: continue
print i*2
Thank you for replying, Mitja! That *is* a nice alternative.
Do you think it''s a good idea to ask on comp.python.devel if they would
be interested in a PEP about this (provided there is none)?
Cheers,
Sergey.
On 2006-03-26, s.*********@gmail.com <s.*********@gmail.com> wrote:Hi All,
I apologize if this was brought up before, I couldn''t find any "prior
art" :-).
On more than one occasion, I found myself wanting to use a "conditional
loop" like this (with "Invalid syntax" error, of course):
for i in c if <test>:
print i*2
...because it''s similar to the list comprehension construct:
[i*2 for i in c if <test>]
---------
Is this the intended difference in constructs? The available equivalent
feels a bit awkward:
for i in c:
if <test>:
print i*2
for j in [i*2 for i in c if <test>]:
print j
--
Grant Edwards grante Yow! .. I wonder if I
at ought to tell them about my
visi.com PREVIOUS LIFE as a COMPLETE
STRANGER?
这篇关于"对于"循环和列表理解相似性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!