Run Length Encoding in Python with List Comprehension

前端 未结 2 1370
感动是毒
感动是毒 2020-12-19 12:50

I have a more basic Run Length Encoding question compared to many of the questions about this topic that have already been answered. Essentially, I\'m trying to take the str

2条回答
  •  天命终不由人
    2020-12-19 13:08

    You can use itertools.groupby():

    from itertools import groupby
    
    grouped = [list(g) for k, g in groupby(string)]
    

    This will produce your per-letter groups as a list of lists.

    You can turn that into a RLE in one step:

    rle = ''.join(['{}{}'.format(k, sum(1 for _ in g)) for k, g in groupby(string)])
    

    Each k is the letter being grouped, each g an iterator producing N times the same letter; the sum(1 for _ in g) expression counts those in the most efficient way possible.

    Demo:

    >>> from itertools import groupby
    >>> string = 'aabccccaaa'
    >>> [list(g) for k, g in groupby(string)]
    [['a', 'a'], ['b'], ['c', 'c', 'c', 'c'], ['a', 'a', 'a']]
    >>> ''.join(['{}{}'.format(k, sum(1 for _ in g)) for k, g in groupby(string)])
    'a2b1c4a3'
    

提交回复
热议问题