当x(x)时,x为xrange(x)中的x [英] [ x for x in xrange(10) when p(x) ]

查看:72
本文介绍了当x(x)时,x为xrange(x)中的x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想知道是否有这样的事情,因为python正在远离

FP函数,如dropwhile / takewhile等。

解决方案

bo **** @ gmail。 com < bo **** @ gmail.com>写道:



我想知道是否有这样的事情,因为python正在远离


这通过写''如果'而不是

''''来成为有效的列表理解。

FP函数如dropwhile / takewhile等。



没办法 - itertools模块是并且仍然是一个珍贵的资源。如果

你想要一个迭代器而不是一个列表,那么itertools.ifilter在这里是相当的



Alex



" Alex Martelli"写道:

bon ... @ gmail.com< bon ... @ gmail.com>写道:

FP函数如dropwhile / takewhile等。



没办法 - itertools模块是并且仍然是一个珍贵的资源。如果你想要一个迭代器而不是一个列表,那么itertools.ifilter非常适合这里。




python 3K中itertools的未来怎么样? IIRC,目前返回列表的几个

函数和方法将返回

迭代器。这可能意味着itertools。(imap / ifilter / izip)将取代地图/过滤器/拉链作为内置物吗?


George




Alex Martelli写道:

通过编写if来改为有效的列表理解
''when''。



有效,是的。效率很高,我不确定。


[x代表x x(10000000)如果p(x)]


意味着我需要即使p = lambda x:x


Hi,

I am wondering if there is such a thing, as python is moving away from
FP functions like dropwhile/takewhile etc.

解决方案

bo****@gmail.com <bo****@gmail.com> wrote:

Hi,

I am wondering if there is such a thing, as python is moving away from
This becomes a valid list comprehension by writing ''if'' instead of
''when''.
FP functions like dropwhile/takewhile etc.



No way -- the itertools module is and remains a PRECIOUS resource. If
you want an iterator rather than a list, itertools.ifilter is quite
appropriate here.
Alex



"Alex Martelli" wrote:

bon...@gmail.com <bon...@gmail.com> wrote:

FP functions like dropwhile/takewhile etc.



No way -- the itertools module is and remains a PRECIOUS resource.
If you want an iterator rather than a list, itertools.ifilter is quite
appropriate here.



What about the future of itertools in python 3K ? IIRC, several
functions and methods that currently return lists are going to return
iterators. Could this imply that itertools.(imap/ifilter/izip) will
take the place of map/filter/zip as builtins ?

George



Alex Martelli wrote:

This becomes a valid list comprehension by writing ''if'' instead of
''when''.


valid, yes. efficient, I am not sure.

[ x for x in xrange(10000000) if p(x) ]

means I need to go through the whole range even if p = lambda x: x < 2.


这篇关于当x(x)时,x为xrange(x)中的x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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