带内存的迭代器?

发布于 2024-11-30 13:49:11 字数 550 浏览 0 评论 0原文

我正在开发一个使用马尔可夫链的应用程序。

此代码的示例如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

も星光 2024-12-07 13:49:11

window 一次会为您提供 iterable 中的 n 个项目。

from collections import deque

def window(iterable, n=3):
    it = iter(iterable)
    d = deque(maxlen = n)
    for elem in it:
        d.append(elem)
        yield tuple(d)


print [x for x in window([1, 2, 3, 4, 5])]
# [(1,), (1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5)]

如果你想要相同数量的物品,即使是前几次,

from collections import deque
from itertools import islice

def window(iterable, n=3):
    it = iter(iterable)
    d = deque((next(it) for Null in range(n-1)), n)
    for elem in it:
        d.append(elem)
        yield tuple(d)


print [x for x in window([1, 2, 3, 4, 5])]

也会这样做。

window will give you n items from iterable at a time.

from collections import deque

def window(iterable, n=3):
    it = iter(iterable)
    d = deque(maxlen = n)
    for elem in it:
        d.append(elem)
        yield tuple(d)


print [x for x in window([1, 2, 3, 4, 5])]
# [(1,), (1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5)]

If you want the same number of items even the first few times,

from collections import deque
from itertools import islice

def window(iterable, n=3):
    it = iter(iterable)
    d = deque((next(it) for Null in range(n-1)), n)
    for elem in it:
        d.append(elem)
        yield tuple(d)


print [x for x in window([1, 2, 3, 4, 5])]

will do that.

﹏半生如梦愿梦如真 2024-12-07 13:49:11
seq = [1,2,3,4,5,6,7]
for w in zip(seq, seq[1:]):
  print w

您还可以执行以下操作来创建任意大小的对:

tuple_size = 2
for w in zip(*(seq[i:] for i in range(tuple_size)))
  print w

编辑:但使用迭代 zip 可能会更好:

from itertools import izip

tuple_size = 4
for w in izip(*(seq[i:] for i in range(tuple_size)))
  print w

我在我的系统上尝试了此操作,其中 seq 为 10,000,000 个整数,结果相当即时。

seq = [1,2,3,4,5,6,7]
for w in zip(seq, seq[1:]):
  print w

You can also do the following to create an arbitrarily-sized pairs:

tuple_size = 2
for w in zip(*(seq[i:] for i in range(tuple_size)))
  print w

edit: But it's probably better using the iterative zip:

from itertools import izip

tuple_size = 4
for w in izip(*(seq[i:] for i in range(tuple_size)))
  print w

I tried this on my system with seq being 10,000,000 integers and the results were fairly instant.

计㈡愣 2024-12-07 13:49:11

改进 yan 的答案以避免复制:

from itertools import *

def staggered_iterators(sequence, count):
  iterator = iter(sequence)
  for i in xrange(count):
    result, iterator = tee(iterator)
    yield result
    next(iterator)

tuple_size = 4
for w in izip(*(i for i in takewhile(staggered_iterators(seq, order)))):
  print w

Improving upon yan's answer to avoid copies:

from itertools import *

def staggered_iterators(sequence, count):
  iterator = iter(sequence)
  for i in xrange(count):
    result, iterator = tee(iterator)
    yield result
    next(iterator)

tuple_size = 4
for w in izip(*(i for i in takewhile(staggered_iterators(seq, order)))):
  print w
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文