提案:[... for ... while cond(x)] [英] Proposal: [... for ... while cond(x)]
问题描述
我建议列表理解语法的新扩展:
[x代表xs xs而cond(x)]
这相当于
列表(itertools.takewhile(cond,xs))
+因为Python支持列表推导而不是地图,过滤,并减少,
这将是这样做的首选方式
+Takewhile operations"经常发生,至少对我而言
+我认为它不会破坏任何现有的语法
dropwhile的类似语法会很好,但是我不能想到
一个。
这不是PEP,因为它是一个非常简单的想法,可能不仅仅是
任何人(读:我)都可以写一个并提交一个。如果有一个PEP for
这个,我错过了它;如果没有,如果有人写了一个就好了。
讨论。
I suggest a new extension of the list comprehension syntax:
[x for x in xs while cond(x)]
which would be equivalent to
list(itertools.takewhile(cond, xs))
+ Since Python favors list comprehensions over map, filter, and reduce,
this would be the preferred way to do this
+ "Takewhile operations" occur often, at least for me
+ I don''t think it would break any existing syntax
An analogous syntax for dropwhile would be nice, but I can''t think of
one.
This is not a PEP because it''s a very simple idea and probably not just
anyone (read: me) can write and submit one. If there has been a PEP for
this, I''ve missed it; if not, it would be nice if someone wrote one.
Discuss.
推荐答案
Eighty写道:
Eighty wrote:
我建议列表理解语法的新扩展名:
[x代表xs x cond(x)]
这相当于
list(itertools.takewhile(cond,xs))
I suggest a new extension of the list comprehension syntax:
[x for x in xs while cond(x)]
which would be equivalent to
list(itertools.takewhile(cond, xs))
这个语法会提供什么:
[x for x in takewhile(cond,xs)]
目前还没提供? (除此之外,就是保存你的3个字符
打字)
What would this syntax offer that:
[x for x in takewhile(cond, xs)]
doesn''t currently offer? (Apart, that is, from saving you 3 characters of
typing)
Duncan Booth写道:
Duncan Booth wrote:
Eighty写道:
Eighty wrote:
我建议列表理解语法的新扩展名:
[x代表xs xs而cond(x)]
这相当于
list( itertools.takewhile(cond,xs))
I suggest a new extension of the list comprehension syntax:
[x for x in xs while cond(x)]
which would be equivalent to
list(itertools.takewhile(cond, xs))
这种语法会提供什么:
[x for x in takewhile (cond,xs)]
目前还没提供? (除此之外,就是从保存你的3个字符
打字)
What would this syntax offer that:
[x for x in takewhile(cond, xs)]
doesn''t currently offer? (Apart, that is, from saving you 3 characters of
typing)
同样的事情[f(x)for x in xs]提供了地图(f,xs)不,
和[x为x在x中如果f(x)]提供过滤器的相同的事情(f,
xs)不是。它更像是pythonic。您可以使用表达式来表示cond
而不是lambda。
The same thing that [f(x) for x in xs] offers that map(f, xs) doesn''t,
and the same thing that [x for x in xs if f(x)] offers that filter(f,
xs) doesn''t. It''s more "pythonic". You can use an expression for cond
instead of a lambda.
Eighty写道:
Eighty wrote:
>
Duncan Booth写道:
>
Duncan Booth wrote:
> Eighty写道:
>Eighty wrote:
我建议使用列表推导语法的新扩展:
[x代表x中的x而cond(x)]
这相当于
list(itertools.takewhile(cond,xs))
I suggest a new extension of the list comprehension syntax:
[x for x in xs while cond(x)]
which would be equivalent to
list(itertools.takewhile(cond, xs))
这个语法是什么提供:
[x for x in takewhile(cond,xs)]
目前还没提供? (除此之外,就是保存你输入的3个字符)
What would this syntax offer that:
[x for x in takewhile(cond, xs)]
doesn''t currently offer? (Apart, that is, from saving you 3
characters of typing)
同样的事情[x(x)x中的x]提供了map(f,xs)不是,
和[x代表x(如果f(x)]中的x提供过滤器相同的事情(f,
) xs)不是。它更像是pythonic。您可以使用表达式来表示cond
而不是lambda。
The same thing that [f(x) for x in xs] offers that map(f, xs) doesn''t,
and the same thing that [x for x in xs if f(x)] offers that filter(f,
xs) doesn''t. It''s more "pythonic". You can use an expression for cond
instead of a lambda.
不,列表推导允许您编写表达式直接避免
函数调用,它还允许您添加一个条件,该条件可以是用于提交序列的
。你的建议什么也没有增加。
No, the list comprehension lets you write an expression directly avoiding a
function call, and it also allows you to add in a condition which can be
used to filer the sequence. Your proposal adds nothing.
这篇关于提案:[... for ... while cond(x)]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!