itertools.ilen? [英] itertools.ilen?

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

问题描述

有时我发现自己只是想要一个迭代器的长度。对于

的例子,收集一些(有点无用;))有关程序的统计数据
我的b $ b,我有这样的代码:


objs = gc.get_objects()

classes = len([objs中obj的obj如果inspect.isclass(obj)])

functions = len([objs objs中的obj,如果inspect.isroutine(obj)])

modules = len([obj for objs objs,如果inspect.ismodule(obj)])

dicts = len([objs中的obj,如果类型为objs(obj)== types.DictType])

lists = len(objs中obj的obj,如果type(obj)== types .ListType])

元组= len(objs中的obj,如果类型为objs = obj)== types.TupleType])


现在,显然我可以(而且,现在2.3正式发布:) :)

用itertools.ifilter替换列表推导,但我需要一个

itertools.ilen来查找长度这样的迭代器。


我可以想象这种需求出现在比这更有用的情况下,但

这是特殊情况需要注意。


Python代码很简单,很明显:


def ilen(iterator):

i = 0

for _ in iterator:

i + = 1

返回i


但是很遗憾使用itertools的超快速迭代器并且必须使用慢速的原始Python来确定它们的长度:)


Jeremy

Sometimes I find myself simply wanting the length of an iterator. For
example, to collect some (somewhat useless ;)) statistics about a program
of mine, I''ve got code like this:

objs = gc.get_objects()
classes = len([obj for obj in objs if inspect.isclass(obj)])
functions = len([obj for obj in objs if inspect.isroutine(obj)])
modules = len([obj for obj in objs if inspect.ismodule(obj)])
dicts = len([obj for obj in objs if type(obj) == types.DictType])
lists = len([obj for obj in objs if type(obj) == types.ListType])
tuples = len([obj for obj in objs if type(obj) == types.TupleType])

Now, obviously I can (and will, now that 2.3 is officially released :))
replace the list comprehensions with itertools.ifilter, but I need an
itertools.ilen to find the length of such iterators.

I can imagine such a need arises in more useful situations than this, but
this is the particular case that brought the need to mind.

The Python code is simple, obviously:

def ilen(iterator):
i = 0
for _ in iterator:
i += 1
return i

But it''s a pity to use itertools'' super-fast iterators and have to use slow,
raw Python to determine their length :)

Jeremy

推荐答案



" Jeremy Fincher" <音响******* @ osu.edu>在消息中写道

news:bg ********** @ news.cis.ohio-state.edu ...

"Jeremy Fincher" <fi*******@osu.edu> wrote in message
news:bg**********@news.cis.ohio-state.edu...
有时我发现自己只是想要迭代器的长度。


迭代器是一个函数/方法,遍历(或可能

生成)一个seqeuence。序列有一个长度(实际或

潜力),但迭代器没有。

例如,收集一些(有些无用;))
统计信息我的程序,我有这样的代码:

objs = gc.get_objects()
classes = len(objs中的obj,objs如果inspect.isclass(obj) ])
functions = len([obj for obj in objs if
inspect.isroutine(obj)])modules = len([obj for obj in objs in objs if b $ b inspect.ismodule(obj)) ])dicts = len([obj in obj in objs if type(obj)==
types.DictType])lists = len([obj for obj in objs if objs if type(obj)==
types .ListType])tuples = len([obj for objs objs if type(obj)==
types.TupleType])


替代方案:将六个计数器初始化为0。扫描列表一次并更新

适当的计数器。

现在,显然我可以(现在将2.3正式发布
:))替换列表推导用itertools.ifi lter,但我需要
一个itertools.ilen来查找这些迭代器的长度。


你的意思是相关的序列。

我可以想象这种需求出现在比
这更有用的情况下,但这是特例这需要牢记。

Python代码很简单,显然:

def ilen(iterator):
i = 0
for _在迭代器中:
我+ = 1
返回我

但是很遗憾使用itertools的'超快速迭代器并且必须使用
使用慢,原始Python确定它们的长度:)
Sometimes I find myself simply wanting the length of an iterator.
An iterator is a function/method that traverses (or possibly
generates) a seqeuence. The sequence has a length (actual or
potential) but the iterator does not.
For example, to collect some (somewhat useless ;))
statistics about a program of mine, I''ve got code like this:

objs = gc.get_objects()
classes = len([obj for obj in objs if inspect.isclass(obj)])
functions = len([obj for obj in objs if inspect.isroutine(obj)]) modules = len([obj for obj in objs if inspect.ismodule(obj)]) dicts = len([obj for obj in objs if type(obj) == types.DictType]) lists = len([obj for obj in objs if type(obj) == types.ListType]) tuples = len([obj for obj in objs if type(obj) == types.TupleType])

Alternative: initialize six counters to 0. Scan list once and update
appropriate counter.
Now, obviously I can (and will, now that 2.3 is officially released :)) replace the list comprehensions with itertools.ifilter, but I need an itertools.ilen to find the length of such iterators.
You mean the associated sequence.
I can imagine such a need arises in more useful situations than this, but this is the particular case that brought the need to mind.

The Python code is simple, obviously:

def ilen(iterator):
i = 0
for _ in iterator:
i += 1
return i

But it''s a pity to use itertools'' super-fast iterators and have to use slow, raw Python to determine their length :)




如果你的意思是一个c编码的计数器(它本身不是一个迭代器)

等价物对于上述情况,可以做到。也许len()可以升级/扩展以接受迭代器并计算它何时无法调用

__len__方法。主要的缺点是迭代器有时候是破坏性的(仅运行一次)。


同时,这真的是你的瓶颈吗?或者仅仅是当一个节目在0.1秒内运行的时候可以获得一个节目吗?


Terry J. Reedy



If you mean a c-coded counter (which would not be an iterator itself)
equivalent to the above, that could be done. Perhaps len() could be
upgraded/extended to accept an iterator and count when it can''t get a
__len__ method to call. The main downside is that iterators are
sometimes destructive (run once only).

In the meanwhile, is this really a bottleneck for you? or merely the
''pity'' of a program running in 1 sec when 0.1 is possible?

Terry J. Reedy




" Terry Reedy" < TJ ***** @ udel.edu> schrieb im Newsbeitrag

news:tp ******************** @ comcast.com ...

"Terry Reedy" <tj*****@udel.edu> schrieb im Newsbeitrag
news:tp********************@comcast.com...
< 杰里米·芬奇(Jeremy Fincher) <音响******* @ osu.edu>在消息中写道
新闻:bg ********** @ news.cis.ohio-state.edu ...

"Jeremy Fincher" <fi*******@osu.edu> wrote in message
news:bg**********@news.cis.ohio-state.edu...
有时我发现自己只是想要长度迭代器。
Sometimes I find myself simply wanting the length of an iterator.



迭代器是遍历(或可能生成)seqeuence的函数/方法。序列有一个长度(实际或
势)但迭代器没有。



An iterator is a function/method that traverses (or possibly
generates) a seqeuence. The sequence has a length (actual or
potential) but the iterator does not.




很好解释。有很多有用的生成器具有无限的

序列。


- 随机生成器


- def achilles() :

而1

:N = 1.

收益率N

n = n / 2


- def schoenberg():

周期=范围(12)

而1:

shuffle(周期)

for i in cycle:

yield i

没有办法确定,这些genrartors是否会来一个

end - 图灵机的暂停问题;-)


亲切的/>
Michael



Very well explained. There are lots of usefull generators with unlimited
sequences.

- random generators

- def achilles():
while 1
:N=1.
yield N
n=n/2

- def schoenberg():
cycle=range(12)
while 1:
shuffle(cycle)
for i in cycle:
yield i
There is no way to determined, whether such generartors will come to an
end - The Halting Problem for Turing Machines ;-)
Thus there will never be a safe len(iterator).

Kindly
Michael


Terry Reedy写道:
Terry Reedy wrote:
迭代器是一个遍历(或可能是
的函数/方法)生成)一个序列。序列有一个长度(实际或
潜力),但迭代器没有。


即使是一些序列也没有长度;考虑(Lisp术语)

不当列表,其中cdr指向列表中较早的单元格。或者

任何类别以某种方式非终止__len__。

备选:将六个计数器初始化为0.扫描列表一次并更新
适当的计数器。


是的,这适用于这种特殊情况,可能是一个优越的

解决方案。

如果你的意思是c-编码计数器(它本身不是迭代器)
等同于上面的,可以做到的。也许len()可以升级/扩展以接受迭代器,并且当它无法调用
__len__方法时进行计数。主要的缺点是迭代器有时是破坏性的(仅运行一次)。


这就是为什么我不认为应该对len()做出这样的改变; *所有*

迭代器是破坏性的,而len()默默地摧毁它们似乎没有什么好看。

通常对潜在的错误有用。

与此同时,这真的是你的瓶颈吗?或者只是在0.1秒内运行时程序运行的程序是什么?
An iterator is a function/method that traverses (or possibly
generates) a seqeuence. The sequence has a length (actual or
potential) but the iterator does not.
Even some sequences don''t have a length; consider (Lisp terminology)
"improper lists," where the cdr points to a cell earlier in the list. Or
any class with a somehow non-terminating __len__.
Alternative: initialize six counters to 0. Scan list once and update
appropriate counter.
Yes, that works in this particular case, and is probably a superior
solution.
If you mean a c-coded counter (which would not be an iterator itself)
equivalent to the above, that could be done. Perhaps len() could be
upgraded/extended to accept an iterator and count when it can''t get a
__len__ method to call. The main downside is that iterators are
sometimes destructive (run once only).
That''s why I don''t think such a change should be made to len(); *all*
iterators are destructive and len() silently destroying them doesn''t seem
generally useful enough for the potential for mistake.
In the meanwhile, is this really a bottleneck for you? or merely the
''pity'' of a program running in 1 sec when 0.1 is possible?




整个itertools似乎真的存在,因为怜悯获取

高效迭代器并将它们转换为列表以便对它们进行任何重大操作。在那种情况下,我会想象为了确定基础序列的长度足够的原因,必须将一个交互器转换为一个序列,这是可惜的。 br />

Jeremy



The whole of itertools really seems to exist because of the "pity" of taking
efficient iterators and turning them into lists in order to do any
significant manipulation of them. In that case, I would imagine the pity
of having to turn an interator into a sequence in order to determine the
length of the underlying sequence would be reason enough.

Jeremy


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

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