interleave string

Discussion in 'Python' started by Andrea Crotti, Feb 15, 2011.

  1. 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?
     
    Andrea Crotti, Feb 15, 2011
    #1
    1. Advertising

  2. 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.
     
    Wojciech Mu³a, Feb 15, 2011
    #2
    1. Advertising

  3. Andrea Crotti

    Alex Willmer Guest

    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.
     
    Alex Willmer, Feb 15, 2011
    #3
  4. Andrea Crotti

    alex23 Guest

    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'
     
    alex23, Feb 16, 2011
    #4
  5. 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
     
    Steven D'Aprano, Feb 16, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    767
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,736
    Mladen Adamovic
    Dec 5, 2003
  3. Matt
    Replies:
    3
    Views:
    541
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Bruce Sam
    Replies:
    15
    Views:
    8,035
    John C. Bollinger
    Nov 19, 2004
  5. Chirayu Krishnappa

    Interleave merge pdf files

    Chirayu Krishnappa, Jul 22, 2005, in forum: Python
    Replies:
    2
    Views:
    4,705
Loading...

Share This Page