RLE.py - a simple run length encoder

Author:
Release:$Id$
Date:December 09, 2013
Tags:Python

Taken from: http://rosettacode.org/wiki/Run-length_encoding#Python

RLE.encode(input_array)

encode array or string.

return tuples of (count, value).

>>> encode(array.array( "i", (10,10,10,10,20,20,20,20) ) )
[(4, 10), (4, 20)]
>>> encode("aaaaahhhhhhmmmmmmmuiiiiiiiaaaaaa")
[(5, 'a'), (6, 'h'), (7, 'm'), (1, 'u'), (7, 'i'), (6, 'a')]
RLE.decode(lst, typecode)

decode to array

>>> decode( [(4, 10), (4, 20)], typecode="i" )
array('i', [10, 10, 10, 10, 20, 20, 20, 20])
>>> decode( [(5, 'a'), (6, 'h'), (7, 'm'), (1, 'u'), (7, 'i'), (6, 'a')], typecode="c" )
array('c', 'aaaaahhhhhhmmmmmmmuiiiiiiiaaaaaa')
RLE.compress(input_string, bytes=1)

return compressed stream.

Previous topic

<no title>

Next topic

<no title>

This Page