Encoding/Decoding
Tools for encoding/decoding sequences.
compress
¶
compress(data, key=None)
compresses the sequence data
by encoding continuous identical items to a tuple of item and count, according to run-length encoding.
Warning
Different from itertools.compress
.
list(compress([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])) # [(1, 1), (2, 2), (3, 3), (4, 4)]
decompress
¶
decompress(data)
decompresses the sequence data
by decoding each tuple of item and count to continuous identical items, according to run-length encoding.
list(decompress([(1, 1), (2, 2), (3, 3), (4, 4)])) # [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
todeltas
¶
todeltas(data, op=operator.sub)
compresses the sequence by encoding the difference between previous and current items, according to delta encoding.
- For custom type of item, either define the
-
operator or specify theop
function computing the difference.
list(todeltas([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])) # [1, 1, 0, 1, 0, 0, 1, 0, 0, 0]
fromdeltas
¶
fromdeltas(data, op=operator.add)
decompresses the sequence by decoding the difference between previous and current items, according to delta encoding.
- For custom type of item, either define the
+
operator or specify theop
function merging the difference.
list(fromdeltas([1, 1, 0, 1, 0, 0, 1, 0, 0, 0])) # [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]