473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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
Jul 29 '07 #1
1 1558
On Jul 28, 9:46 pm, Andrew Savige <ajsav...@yahoo.com.auwrote:
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
Jul 29 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
10
by: mb | last post by:
I was wondering if there is an easy, more useful Split function that will split with a string delimiter like "<>" or "////"?
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
8
by: J Stoodley | last post by:
I am in a learning curve right now, and want to become well aquanted with VB.NET. So, I have two questions. 1 is technial the other is resource related. 1. I need to strip a single character...
4
by: Crirus | last post by:
There is a function somewhere to split a string with multiple tokens at a time? Say I have this: aaaa#bbbbb*ccccc$dddd I whould like to split it so the result whould be aaaa bbb
14
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.