collections.deque is for this an optimized datatype.
It has
You can also initialize the deque with
Example with moving average:
It has
appendleft
, popleft
, pop
, extendleft
and rotate
.You can also initialize the deque with
maxlen
, which is the maximum size.Example with moving average:
import collections def moving_average(iterable, size): q = collections.deque(maxlen=size) for element in iterable: q.append(element) yield sum(q) / len(q) r20 = range(20) list(moving_average(r20, 5))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!