interleave string

Valentin Baciu valentin at syntactic.org
Tue Feb 15 05:19:15 EST 2011


Hello,

How about this:

>>> str = 'xxaabbddee'
>>> ':'.join(map(''.join, zip(str[::2], str[1::2])))

In my example, it should not matter that the letters are repeating.

On Tue, Feb 15, 2011 at 11:53 AM, Andrea Crotti
<andrea.crotti.0 at gmail.com>wrote:

> Just a curiosity not a real problem, I want to pass from a string like
>
> xxaabbddee
> to
> xx:aa:bb:dd:ee
>
> so every two characters insert a ":".
> At the moment I have this ugly inliner
>        interleaved = ':'.join(orig[x:x+2] for x in range(0, len(orig), 2))
>
> but also something like this would work
> [''.join((x,y)) for x, y in zip(orig[0::2], orig[1::2])]
>
> any other ideas?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110215/03c05a63/attachment-0001.html>


More information about the Python-list mailing list