Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 29th, 2007, 05:55 AM
Andrew Savige
Guest
 
Posts: n/a
Default Split a string based on change of character

Python beginner here.

For a string 'ABBBCC', I want to produce a list ['A', 'BBB', 'CC'].
That is, break the string into pieces based on change of character.
What's the best way to do this in Python?

Using Python 2.5.1, I tried:

import re
s = re.split(r'(?<=(.))(?!\1)', 'ABBBCC')
for e in s: print e

but was surprised when it printed:

ABBBCC

I expected something like:

A
A
BBB
B
CC
C

(the extra fields because of the capturing parens).

Thanks,
/-\



__________________________________________________ __________________________________
Yahoo!7 Mail has just got even bigger and better with unlimited storage on all webmail accounts.
http://au.docs.yahoo.com/mail/unlimitedstorage.html
  #2  
Old July 29th, 2007, 06:35 AM
attn.steven.kuo@gmail.com
Guest
 
Posts: n/a
Default Re: Split a string based on change of character

On Jul 28, 9:46 pm, Andrew Savige <ajsav...@yahoo.com.auwrote:
Quote:
Python beginner here.
>
For a string 'ABBBCC', I want to produce a list ['A', 'BBB', 'CC'].
That is, break the string into pieces based on change of character.
What's the best way to do this in Python?
>
Using Python 2.5.1, I tried:
>
import re
s = re.split(r'(?<=(.))(?!\1)', 'ABBBCC')
for e in s: print e
>
but was surprised when it printed:
>
ABBBCC
>
I expected something like:
>
A
A
BBB
B
CC
C
>
(the extra fields because of the capturing parens).

Using itertools:

import itertools

s = 'ABBBCC'
print [''.join(grp) for key, grp in itertools.groupby(s)]


Using re:

import re

pat = re.compile(r'((\w)\2*)')
print [t[0] for t in re.findall(pat, s)]


By the way, your pattern seems to work in perl:

$ perl -le '$, = " "; print split(/(?<=(.))(?!\1)/, "ABBBCC");'
A A BBB B CC C

Was that the type of regular expressions you were expecting?

--
Hope this helps,
Steven


 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles