[Python-ideas] Run length encoding

Joshua Morton joshua.morton13 at gmail.com
Sat Jun 10 23:27:34 EDT 2017


David: You're absolutely right, s/2/3 in my prior post!

Neal: As for why zip (at first I thought you meant the zip function, not
the zip compression scheme) is included and rle is not, zip is (or was), I
believe, used as part of python's packaging infrastructure, hopefully
someone else can correct me if that's untrue.

--Josh

On Sat, Jun 10, 2017 at 8:20 PM David Mertz <mertz at gnosis.cx> wrote:

> God no! Not in the Python 2 docs! ... if the recipe belongs somewhere it's
> in the Python 3 docs.  Although, I suppose it could go under 2 also, since
> it's not actually a behavior change in the feature-frozen interpreter.  But
> as a Python instructor (and someone who remembers the cool new features of
> Python 1.5 over 1.4 pretty well), my attitude about Python 2 is "kill it
> with fire!"
>
> Your spelling of the one-liner is prettier, shorter, and more intuitive
> than mine, and the same speed.
>
> On Sat, Jun 10, 2017 at 8:12 PM, Joshua Morton <joshua.morton13 at gmail.com>
> wrote:
>
>> Another is
>>
>> [(k, len(list(g))) for k, g in groupby(l)]
>>
>>
>> It might be worth adding it to the list of recipies either at
>> https://docs.python.org/2/library/itertools.html#itertools.groupby or at
>> https://docs.python.org/2/library/itertools.html#recipes, though.
>>
>> On Sat, Jun 10, 2017 at 8:07 PM David Mertz <mertz at gnosis.cx> wrote:
>>
>>> Here's a one-line version:
>>>
>>> from itertools import groupby
>>> rle_encode = lambda it: (
>>>     (l[0],len(l)) for g in groupby(it) for l in [list(g[1])])
>>>
>>> Since "not every one line function needs to be in the standard library"
>>> is a guiding principle of Python, and even moreso of `itertools`, probably
>>> this is a recipe in the documentation at most.  Or maybe it would have a
>>> home in `more_itertools`.
>>>
>>>
>>> On Sat, Jun 10, 2017 at 7:20 PM, Neal Fultz <nfultz at gmail.com> wrote:
>>>
>>>> Hello python-ideas,
>>>>
>>>> I am very new to this, but on a different  forum and after a couple
>>>> conversations, I really wished Python came with run-length encoding
>>>> built-in; after all, it ships with zip, which is much more complicated :)
>>>>
>>>> The general idea is to be able to go back and forth between two
>>>> representations of a sequence:
>>>>
>>>> [1,1,1,1,2,3,4,4,3,3,3]
>>>>
>>>> and
>>>>
>>>> [(1, 4), (2, 1), (3, 1), (4, 2), (3, 3)]
>>>>
>>>> where the first element is the data element, and the second is how many
>>>> times it is repeated.
>>>>
>>>> I wrote an encoder/decoder in about 20 lines (
>>>> https://github.com/nfultz/rle.py/blob/master/rle.py ) and would like
>>>> to offer it for the next version; I think it might fit in nicely in the
>>>> itertools module, for example. I am curious about your thoughts.
>>>>
>>>> Best,
>>>>
>>>> -Neal
>>>>
>>>> _______________________________________________
>>>> Python-ideas mailing list
>>>> Python-ideas at python.org
>>>> https://mail.python.org/mailman/listinfo/python-ideas
>>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>>
>>>>
>>>
>>>
>>> --
>>> Keeping medicines from the bloodstreams of the sick; food
>>> from the bellies of the hungry; books from the hands of the
>>> uneducated; technology from the underdeveloped; and putting
>>> advocates of freedom in prisons.  Intellectual property is
>>> to the 21st century what the slave trade was to the 16th.
>>> _______________________________________________
>>> Python-ideas mailing list
>>> Python-ideas at python.org
>>> https://mail.python.org/mailman/listinfo/python-ideas
>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>
>>
>
>
> --
> Keeping medicines from the bloodstreams of the sick; food
> from the bellies of the hungry; books from the hands of the
> uneducated; technology from the underdeveloped; and putting
> advocates of freedom in prisons.  Intellectual property is
> to the 21st century what the slave trade was to the 16th.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170611/6bbc9289/attachment.html>


More information about the Python-ideas mailing list