FAQ
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?

Search Discussions

  • Wojciech Muła at Feb 15, 2011 at 10:09 am

    On Tue, 15 Feb 2011 10:53:56 +0100 Andrea Crotti 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?
    import re

    s = 'xxaabbddee'
    m = re.compile("(..)")
    s1 = m.sub("\\1:", s)[:-1]

    w.
  • Alex Willmer at Feb 15, 2011 at 1:21 pm

    On Feb 15, 10:09?am, Wojciech Mu?a wrote:
    import re

    s = 'xxaabbddee'
    m = re.compile("(..)")
    s1 = m.sub("\\1:", s)[:-1]
    One can modify this slightly:

    s = 'xxaabbddee'
    m = re.compile('..')
    s1 = ':'.join(m.findall(s))

    Depending on one's taste this could be clearer. The more general
    answer, from the itertools docs:

    from itertools import izip_longest

    def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

    s2 = ':'.join(''.join(pair) for pair in grouper(2, s, ''))

    Note that this behaves differently to the previous solutions, for
    sequences with an odd length.
  • Valentin Baciu at Feb 15, 2011 at 10:19 am
    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
    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.html>
  • MRAB at Feb 15, 2011 at 3:00 pm

    On 15/02/2011 09:53, Andrea Crotti 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?
    interleaved = "{}{}:{}{}:{}{}:{}{}".format(*orig)
  • Alex23 at Feb 16, 2011 at 3:39 am

    Andrea Crotti wrote:
    At the moment I have this ugly inliner
    ? ? ? ? interleaved = ':'.join(orig[x:x+2] for x in range(0, len(orig), 2))
    I actually prefer this over every other solution to date. If you feel
    its too much behaviour in one line, I sometimes break it out into
    separate values to provide some in-code documentation:
    s = "xxaabbddee"
    get_two_chars_at = lambda i: s[i:i+2]
    string_index = xrange(0, len(s), 2)
    ':'.join(get_two_chars_at(i) for i in string_index)
    'xx:aa:bb:dd:ee'
  • Steven D'Aprano at Feb 16, 2011 at 1:25 pm

    On Tue, 15 Feb 2011 19:39:30 -0800, alex23 wrote:

    Andrea Crotti wrote:
    At the moment I have this ugly inliner
    ? ? ? ? interleaved = ':'.join(orig[x:x+2] for x in range(0,
    ? ? ? ? len(orig), 2))
    I actually prefer this over every other solution to date.
    Agreed. To me, it's the simplest, least contrived way to solve the
    problem.


    --
    Steven

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 15, '11 at 9:53a
activeFeb 16, '11 at 1:25p
posts7
users7
websitepython.org

People

Translate

site design / logo © 2022 Grokbase