Skip to content

Instantly share code, notes, and snippets.

@myrfy001
Last active October 17, 2017 11:10
Show Gist options
  • Save myrfy001/0fc47bac3c450227b8fc989c9d1fa812 to your computer and use it in GitHub Desktop.
Save myrfy001/0fc47bac3c450227b8fc989c9d1fa812 to your computer and use it in GitHub Desktop.
迭代器风格的滑动平均数计算程序
# Author myrfy
def moving_average(seq_in, window_size):
sum = 0.0
iter_ = iter(seq_in)
buf_q = deque(maxlen=window_size)
# build the init window
for i in islice(iter_, window_size):
buf_q.append(i)
sum += i
yield sum / len(buf_q)
for i in iter_:
sum -= buf_q.popleft()
sum += i
buf_q.append(i)
yield sum / window_size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment