itertools.dropwhile()和itertools.takewhile()的命运 [英] Fate of itertools.dropwhile() and itertools.takewhile()

查看:75
本文介绍了itertools.dropwhile()和itertools.takewhile()的命运的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑弃用这两个功能,并希望从社区或具有

功能编程背景的人那里获得一些

的反馈。


*我担心这两个功能的用例并不常见,而且b $ b可能会掩盖代码而不是澄清代码。


*我最初将它们添加到itertools中,因为它们是在其他函数式语言中找到的,因为它们似乎可以与其他itertools一起提供基本构建块
允许

构建各种功能强大的高速迭代器。

后者可能是一个错误的希望 - 到目前为止,我还没有看到好的

食谱依赖于这两种功能。

*如果给出一个始终为true或总是为假的谓词,那么在函数运行后突破该函数可能很难。


*这两个函数看似简单和基本,直到你试图向其他人解释它们b
。同样,当读取包含dropwhile()的代码时,

我不认为dropwhile()可能有一个很长的启动时间。


*由于itertools要组合在一起,如果可供选择的工具较少,整个模块

将变得更容易使用。


这些想法反映了我自己使用itertools模块的经验。

可能你与他们的经历有所不同。请

让我知道你的想法。


雷蒙德

I''m considering deprecating these two functions and would like some
feedback from the community or from people who have a background in
functional programming.

* I''m concerned that use cases for the two functions are uncommon and
can obscure code rather than clarify it.

* I originally added them to itertools because they were found in
other functional languages and because it seemed like they would serve
basic building blocks in combination with other itertools allow
construction of a variety of powerful, high-speed iterators. The
latter may have been a false hope -- to date, I''ve not seen good
recipes that depend on either function.

* If an always true or always false predicate is given, it can be hard
to break-out of the function once it is running.

* Both functions seem simple and basic until you try to explain them
to someone else. Likewise, when reading code containing dropwhile(),
I don''t think it is self-evident that dropwhile() may have a lengthy
start-up time.

* Since itertools are meant to be combined together, the whole module
becomes easier to use if there are fewer tools to choose from.

These thoughts reflect my own experience with the itertools module.
It may be that your experience with them has been different. Please
let me know what you think.

Raymond

推荐答案

12月29日下午6:10,Raymond Hettinger< pyt ... @ rcn.comwrote:
On Dec 29, 6:10 pm, Raymond Hettinger <pyt...@rcn.comwrote:

这些想法反映了我自己对itertools模块的体验。 br />
您可能与他们的经历有所不同。请

让我知道你的想法。
These thoughts reflect my own experience with the itertools module.
It may be that your experience with them has been different. Please
let me know what you think.



首先,itertools模块非常棒,感谢您创建它。它改变了我对编程的看法。
改变了我对编程的看法。事实上,现在我开始

所有我的程序:


来自itertools import *


可能不是最好的形式,但我厌倦了单独导入每个

单个函数或写出模块名称。


现在我从来不需要dropwhile()和takewhile()函数,但是

可能意义不大。很长一段时间我也从不需要重复()

功能。有一个迭代器,即使
只是一遍又一遍地重复同样的事情,这甚至看起来都是荒谬的。有一天,我不得不解决一个需要重复()的问题,并让我真正理解它是什么原因

for并且惊叹于解决方案有多整洁。


i。

first off, the itertools module is amazing, thanks for creating it. It
changed the way I think about programming. In fact nowadays I start
all my programs with:

from itertools import *

which may not be the best form, but I got tired of importing every
single function individually or writing out the module name.

Now I never needed the dropwhile() and takewhile() functions, but that
may not mean much. For quite a while I never needed the repeat()
function either. It even looked nonsensical to have an iterator that
simply repeats the same thing over and over. One day I had to solve a
problem that needed repeat() and made me really understand what it was
for and got to marvel at a just how neat the solution was.

i.


2007年12月29日星期六15:10:24 -0800,Raymond Hettinger写道:
On Sat, 29 Dec 2007 15:10:24 -0800, Raymond Hettinger wrote:

*这两个函数看似简单和基本,直到你试图将它们解释为

其他人。
* Both functions seem simple and basic until you try to explain them to
someone else.



哦,我不知道。 doc字符串似乎对我做了令人钦佩的工作

。与groupby()相比,函数本身就是简单的。

Oh I don''t know about that. The doc strings seem to do an admirable job
to me. Compared to groupby(), the functions are simplicity themselves.


同样,当读取包含dropwhile()的代码时,我

不要认为dropwhile()可能有一个很长的启动时间是不言自明的。
Likewise, when reading code containing dropwhile(), I
don''t think it is self-evident that dropwhile() may have a lengthy
start-up time.



*在混乱中划伤*


这不是吗?我可以理解某人*估计启动时间

(也许是因为他们高估了dropwhile()可以通过项目迭代

的速度)。但是肯定不言而喻,在物品开始返回之前,物品必须丢弃物品吗?

*scratches head in confusion*

It isn''t? I can understand somebody *under*estimating the start-up time
(perhaps because they overestimate how quickly dropwhile() can iterate
through the items). But surely it is self-evident that a function which
drops items has to drop the items before it can start returning?


*由于itertools要组合在一起,如果有更少的工具可供选择,整个模块

将变得更容易使用。
* Since itertools are meant to be combined together, the whole module
becomes easier to use if there are fewer tools to choose from.



是的,但另一方面,使用工具太少的工具箱比使用太多工具的工具箱更难使用

。 />

-

Steven

True, but on the other hand a toolbox with too few tools is harder to use
than one with too many tools.

--
Steven


几乎每天我都会编写使用itertools的代码,所以我发现它非常有用,而且它的功能很快。

删除无用的东西并保持整洁通常是正面的。但是

我不能告诉你要删除什么。这是我的用法(每个子列表是按照倒置频率使用排序的
):


我经常使用或经常使用:

groupby(iterable [,key])

imap(函数,* iterables)

izip(* iterables)

ifilter(谓词,可迭代)

islice(可迭代,[开始,]停止[,步骤])


我曾经使用过一次:

循环(可迭代)

链(* iterables)

count([n])

repeat(object [,times])


我用过一次或几次:

starmap(函数,可迭代)

tee(iterable [,n = 2] )

ifilterfalse(谓词,可迭代)


到目前为止从未使用过:

dropwhile(谓词,可迭代)

takewhile(谓词,可迭代)


再见,

熊宝宝
Almost every day I write code that uses itertools, so I find it very
useful, and its functions fast.
Removing useless things and keeping things tidy is often positive. But
I can''t tell you what to remove. Here are my usages (every sub-list is
sorted by inverted frequency usage):

I use often or very often:
groupby( iterable[, key])
imap( function, *iterables)
izip( *iterables)
ifilter( predicate, iterable)
islice( iterable, [start,] stop [, step])

I use once in while:
cycle( iterable)
chain( *iterables)
count( [n])
repeat( object[, times])

I have used probably one time or few times:
starmap( function, iterable)
tee( iterable[, n=2])
ifilterfalse( predicate, iterable)

Never used so far:
dropwhile( predicate, iterable)
takewhile( predicate, iterable)

Bye,
bearophile


这篇关于itertools.dropwhile()和itertools.takewhile()的命运的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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