Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > interleave string

Reply
Thread Tools

interleave string

 
 
Andrea Crotti
Guest
Posts: n/a
 
      02-15-2011
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+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?
 
Reply With Quote
 
 
 
 
Wojciech Mua
Guest
Posts: n/a
 
      02-15-2011
On Tue, 15 Feb 2011 10:53:56 +0100 Andrea Crotti
<(E-Mail Removed)> 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+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.
 
Reply With Quote
 
 
 
 
Alex Willmer
Guest
Posts: n/a
 
      02-15-2011
On Feb 15, 10:09*am, Wojciech Muła
<(E-Mail Removed)> 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.
 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      02-16-2011
Andrea Crotti <(E-Mail Removed)> wrote:
> At the moment I have this ugly inliner
> * * * * interleaved = ':'.join(orig[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'
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-16-2011
On Tue, 15 Feb 2011 19:39:30 -0800, alex23 wrote:

> Andrea Crotti <(E-Mail Removed)> wrote:
>> At the moment I have this ugly inliner
>> * * * * interleaved = ':'.join(orig[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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interleave merge pdf files Chirayu Krishnappa Python 2 07-22-2005 10:41 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments