问题描述
什么是克隆独立的方法。迭代器?我不能用tee(),
因为我不知道有多少独立我需要的迭代器。复制和
deepcopy无法正常工作......
--pavel
Whats is the way to clone "independent" iterator? I can''t use tee(),
because I don''t know how many "independent" iterators I need. copy and
deepcopy doesn''t work...
--pavel
推荐答案
没有通用方法。对于短而言序列,你可以将项目存储在一个
列表中,这也是tee()的最坏情况。
你想做什么? br />
彼得
There is no general way. For "short" sequences you can store the items in a
list which is also the worst-case behaviour of tee().
What are you trying to do?
Peter
没有通用方法。对于短而言序列,你可以将项目存储在一个
列表中,这也是tee()的最坏情况。
你想做什么?
Peter
There is no general way. For "short" sequences you can store the items ina
list which is also the worst-case behaviour of tee().
What are you trying to do?
Peter
我尝试生成迭代器(迭代器的迭代器)。彼得,你是
吧!谢谢。例如,它可以使用像
这样的东西:
def cloneiter(it):
" "" return(clonable,clone)""
return tee(it)
和用法:
clonable,seq1 = cloneiter(seq)
... iter over seq1 ...
然后再次克隆:
clonable,seq2 = cloneiter(clonable)
... iter over seq2 ...
或者在课堂上:
class ReIter:
def __init __(self,it):
self._it = it
def __iter __(个体经营):
self._it,ret = tee(self._it)
返回
和用法:
ri = ReIter(seq)
... iti over ri ...
......再次过来......
......再次......
但我觉得(我很有信心!)它缺乏Python迭代器!他们是
不太好...
- Pavel
I try to generate iterators (iterator of iterators). Peter, you are
right! Thank you. For example, it''s possible to use something like
this:
def cloneiter( it ):
"""return (clonable,clone)"""
return tee(it)
and usage:
clonable,seq1 = cloneiter(seq)
...iter over seq1...
then clone again:
clonable,seq2 = cloneiter(clonable)
...iter over seq2...
Or in class:
class ReIter:
def __init__( self, it ):
self._it = it
def __iter__( self ):
self._it,ret = tee(self._it)
return ret
and usage:
ri = ReIter(seq)
...iter over ri...
...again iter over ri...
...and again...
But I think (I''m sure!) it''s deficiency of Python iterators! They are
not very good...
--Pavel
没有一般的方法。对于短而言序列,你可以将项目存储在一个列表中,这也是tee()的最坏情况。
你想做什么?
彼得
There is no general way. For "short" sequences you can store the items in
a list which is also the worst-case behaviour of tee().
What are you trying to do?
Peter
我尝试生成迭代器(迭代器的迭代器)。彼得,你是
吧!谢谢。例如,它可以使用像
这样的东西:
def cloneiter(it):
" "" return(clonable,clone)""
return tee(it)
I try to generate iterators (iterator of iterators). Peter, you are
right! Thank you. For example, it''s possible to use something like
this:
def cloneiter( it ):
"""return (clonable,clone)"""
return tee(it)
[snip]
这太抽象了,抱歉。你试图用你的克隆迭代器解决什么具体问题?
?可能有一种方法可以用不需要它们的方式重新安排你的设置。
[snip]
That is too abstract, sorry. What concrete problem are you trying to solve
with your cloned iterators? There might be a way to rearrange your setup in
a way that doesn''t need them.
嗯,我认为Python的迭代器,特别是发生器,很漂亮。 br />
更重要的是,我认为没有通用的方法可以使迭代器
可复制,无论编程语言如何。问题是,大多数有用的bb b取决于外部状态。
Peter
Well, I think Python''s iterators, especially the generators, are beautiful.
More importantly, I think there is no general way to make iterators
copyable, regardless of the programming language. The problem is that most
of the useful ones depend on external state.
Peter
这篇关于迭代器克隆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!