LZW compression: Difference between revisions

Content added Content deleted
Line 4,178: Line 4,178:


=={{header|Python}}==
=={{header|Python}}==
{{works with|Python|3.x}}

In this version the dicts contain mixed typed data:
In this version the dicts contain mixed typed data:
<lang python>def compress(uncompressed):
<lang python>def compress(uncompressed):
Line 4,185: Line 4,185:
# Build the dictionary.
# Build the dictionary.
dict_size = 256
dict_size = 256
dictionary = dict((chr(i), i) for i in xrange(dict_size))
dictionary = dict((chr(i), i) for i in range(dict_size))
# in Python 3: dictionary = {chr(i): i for i in range(dict_size)}
# in Python 3: dictionary = {chr(i): i for i in range(dict_size)}


Line 4,209: Line 4,209:
def decompress(compressed):
def decompress(compressed):
"""Decompress a list of output ks to a string."""
"""Decompress a list of output ks to a string."""
from cStringIO import StringIO
from io import StringIO


# Build the dictionary.
# Build the dictionary.
dict_size = 256
dict_size = 256
dictionary = dict((i, chr(i)) for i in xrange(dict_size))
dictionary = dict((i, chr(i)) for i in range(dict_size))
# in Python 3: dictionary = {i: chr(i) for i in range(dict_size)}
# in Python 3: dictionary = {i: chr(i) for i in range(dict_size)}