Array? Please help.

Diez B. Roggisch deets at nospam.web.de
Sat May 27 20:58:53 CEST 2006


Dr. Pastor schrieb:
> I need a row of 127 bytes that I will use as a
> circular buffer. Into the bytes (at unspecified times)
> a mark (0<mark<128) will be written, one after the other.
> After some time the "buffer" will contain the last 127 marks.
> (A pointer will point to the next byte to write to.)
> What would be the Pythonic way to do the above?
> Thanks for any guidance.

Use a list, use append and slicing on it:


max_size = 10
buffer = []

for i in xrange(100):
     buffer.append(i)
     buffer[:] = buffer[-max_size:]
     print buffer


Diez



More information about the Python-list mailing list