Python solve problem with string operation

John Gordon gordon at panix.com
Thu Jan 16 17:30:27 EST 2014


In <mailman.5607.1389911083.18130.python-list at python.org> Nac Temha <naccttemha at gmail.com> writes:

> --047d7b6d95d0367a3d04f01de490
> Content-Type: text/plain; charset=ISO-8859-1

> Hi everyone,

> I want to do operation with chars in the given string. Actually I want to
> grouping the same chars.

> For example;

> input : "344111133311222223377"
> operation-> (3)(44)(1111)(333)(11)(22222)(33)(77)
> output: "34131237"

input = "344111133311222223377"
output = []
previous_ch = None
for ch in input:
    if ch != previous_ch:
        output.append(ch)
        previous_ch = ch
print ''.join(output)

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon at panix.com    watch 'House', or a real serial killer to watch 'Dexter'.




More information about the Python-list mailing list