带内存的迭代器?
我正在开发一个使用马尔可夫链的应用程序。
此代码的示例如下:
chain = MarkovChain(order=1)
train_seq = ["","hello","this","is","a","beautiful","world"]
for i, word in enum(train_seq):
chain.train(previous_state=train_seq[i-1],next_state=word)
我正在寻找的是迭代 train_seq,但保留最后 N 个元素。
for states in unknown(train_seq,order=1):
# states should be a list of states, with states[-1] the newest word,
# and states[:-1] should be the previous occurrences of the iteration.
chain.train(*states)
希望我的问题的描述足够清楚
I'm working on an application which use a markov chain.
An example on this code follows:
chain = MarkovChain(order=1)
train_seq = ["","hello","this","is","a","beautiful","world"]
for i, word in enum(train_seq):
chain.train(previous_state=train_seq[i-1],next_state=word)
What I am looking for is to iterate over train_seq, but to keep the N last elements.
for states in unknown(train_seq,order=1):
# states should be a list of states, with states[-1] the newest word,
# and states[:-1] should be the previous occurrences of the iteration.
chain.train(*states)
Hope the description of my problem is clear enough for
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
window
一次会为您提供iterable
中的n
个项目。如果你想要相同数量的物品,即使是前几次,
也会这样做。
window
will give youn
items fromiterable
at a time.If you want the same number of items even the first few times,
will do that.
您还可以执行以下操作来创建任意大小的对:
编辑:但使用迭代 zip 可能会更好:
我在我的系统上尝试了此操作,其中 seq 为 10,000,000 个整数,结果相当即时。
You can also do the following to create an arbitrarily-sized pairs:
edit: But it's probably better using the iterative zip:
I tried this on my system with seq being 10,000,000 integers and the results were fairly instant.
改进 yan 的答案以避免复制:
Improving upon yan's answer to avoid copies: