how to split a string (or sequence) into pairs of characters?

Brian McErlean b_mcerlean at yahoo.com
Fri Aug 16 12:30:29 EDT 2002


"Jason R. Coombs" <jaraco at spamblocker.sandia.gov> wrote in message news:<ajgp2v$67c$1 at sass2141.sandia.gov>...
> I have a segment of code that seems particularly ugly.  I want to take a
> sequence and divide it into pairs such that pairs( 'aabbccdd' ) == (
> 'aa','bb','cc','dd' ). Here's code that does what I want.
> 
>   even = range( 0, len( s ), 2 )
>   odd = range( 1, len( s ), 2 )
>   even = map( s.__getitem__, even )
>   odd = map( s.__getitem__, odd )
>   pairs = map( operator.add, even, odd )
> 
> I'd like it to be more elegant and more efficient, and I'd like to avoid
> using loops.  I could obviously do:
> 
>   pairs = []
>   for i in range( 0,len(s),2 ):
>     pairs.append( s[i:i+2] )
> 
> But even that seems a bit inefficient, and doesn't take advantages of the
> powerful sequence operations of Python.
> 
> Can anyone come up with a better way of performing these operations? Extra
> kudos if it easily extends to any sublength and not just pairs.
> 
> Jason

I posted this earlier for a similar request:

class Group:
  def __init__(self, l, size):
    self.size=size
    self.l = l

  def __getitem__(self, group):
    idx = group * self.size
    if idx > len(self.l): 
      raise IndexError("Out of range")
    return self.l[idx:idx+self.size]

for pair in Group('aabbccddee',2):
  # do something with pair.

This works with any sequence type (lists, tuples strings etc), lazy,
can be used for any size and should work on any python version from
1.5.2 on.

Brian.



More information about the Python-list mailing list