473,414 Members | 1,781 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,414 software developers and data experts.

Simple question to split

Hi,

I have a simple question. When I read in a string like:
a='1,2,3,4,5 6 7,3,4', can I get the list l=[1,2,3,4,5,6,7,3,4] with a
single split-call?

Thx,
Matthias
Nov 9 '06 #1
7 1283
Yep, use regular expressions! For example, use the regular expression
r',|\s+' to split on either (a) any amount of whitespace or (b) a
single comma. Try this:

import re
a='1,2,3,4,5 6 7,3,4'
print re.split(r',|\s+*', a)

Matthias Winterland wrote:
Hi,

I have a simple question. When I read in a string like:
a='1,2,3,4,5 6 7,3,4', can I get the list l=[1,2,3,4,5,6,7,3,4] with a
single split-call?

Thx,
Matthias
Nov 9 '06 #2
Matthias Winterland schrieb:
Hi,

I have a simple question. When I read in a string like:
a='1,2,3,4,5 6 7,3,4', can I get the list l=[1,2,3,4,5,6,7,3,4] with a
single split-call?
Nope. But you could replace the commas with spaces, and then split.

Diez
Nov 9 '06 #3
Diez B. Roggisch schrieb:
Matthias Winterland schrieb:
>Hi,

I have a simple question. When I read in a string like:
a='1,2,3,4,5 6 7,3,4', can I get the list l=[1,2,3,4,5,6,7,3,4] with a
single split-call?

Nope. But you could replace the commas with spaces, and then split.
Or use re.split....

Diez
Nov 9 '06 #4
Diez B. Roggisch schrieb:
Diez B. Roggisch schrieb:
>Matthias Winterland schrieb:
>>Hi,

I have a simple question. When I read in a string like:
a='1,2,3,4,5 6 7,3,4', can I get the list l=[1,2,3,4,5,6,7,3,4] with a
single split-call?

Nope. But you could replace the commas with spaces, and then split.

Or use re.split....
And not forget to map it through int() afterwards.

Georg
Nov 9 '06 #5
Matthias Winterland wrote:
Hi,

I have a simple question. When I read in a string like:
a='1,2,3,4,5 6 7,3,4', can I get the list l=[1,2,3,4,5,6,7,3,4] with a
single split-call?
You can't get what you want with a single method call. You can do it with a
single call to .split() if you preprocess the string first:

a.replace(',', ' ').split()

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Nov 9 '06 #6
def splits(seq, cs):
if not cs: return seq
elif isinstance(seq, str): return splits(seq.split(cs[0]), cs[1:])
else: return splits(sum([elem.split(cs[0]) for elem in seq], []),
cs[1:])

or
a = re.split('(\ |\,)', a)
a.remove(' ')
a.remove(',')

Matthias Winterland wrote:
Hi,

I have a simple question. When I read in a string like:
a='1,2,3,4,5 6 7,3,4', can I get the list l=[1,2,3,4,5,6,7,3,4] with a
single split-call?

Thx,
Matthias
Nov 9 '06 #7
Matthias Winterland wrote:
Hi,

I have a simple question. When I read in a string like:
a='1,2,3,4,5 6 7,3,4', can I get the list l=[1,2,3,4,5,6,7,3,4] with a
single split-call?
Using str method split, no -- as documented [hint!], it provides only a
single separator argument.

Using re.split function, yes -- as documented [hint!], it allows a
pattern as a separator.

The required pattern is simply expressed as "space or comma":

| import re
| >>re.split('[, ]', '1,2,3,4,5 6 7,3,4')
| ['1', '2', '3', '4', '5', '6', '7', '3', '4']
| >>[int(x) for x in re.split('[, ]', '1,2,3,4,5 6 7,3,4')]
| [1, 2, 3, 4, 5, 6, 7, 3, 4]

Cheers,
John

Nov 9 '06 #8

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

Similar topics

10
by: george young | last post by:
For each run of my app, I have a known set of (<100) wafer names. Names are sometimes simply integers, sometimes a short string, and sometimes a short string followed by an integer, e.g.: 5, 6,...
1
by: mark leeds | last post by:
i am not a javasript programmer by any stretch but i have been writing a javascript programmer for a friend that does the following : 1) prompts the user for first name, middle name and last...
2
by: nix | last post by:
Hi I want to create a simple user validation script without using a database. Let's say I have a text file in my asp folder with a list of valid usernames. How can i do something like the...
6
by: wl | last post by:
Hi, I'm relatively new to Javascript and wanted to use the split function to split a string into an array. I need to split on every single occurence of = and &. For example:...
1
by: steve smith | last post by:
Hi I have just downloaded the Borland C# Builder and the Micorsoft ..Net framework SDK v1.1 from the borland webist, and i am trying to get a simple program to run, however I keep getting errors,...
10
by: RickMuller | last post by:
One of my all-time favorite scripts is parseline, which is printed below def parseline(line,format): xlat = {'x':None,'s':str,'f':float,'d':int,'i':int} result = words = line.split() for i...
22
by: giordan | last post by:
Hi all! I've wrote this code: <script type="text/javascript"> var largImg; var altImg; var txtTop = '<b>Ottima scelta!</b> Ora compila il form e premi "Ricevi banner". Il...
7
by: AMP | last post by:
Hello, I am trying to split a string at the newline and this doesnt work: String Channel = FileName.Split("\r"); What am I doing wrong? Thanks Mike
0
rnd me
by: rnd me | last post by:
Purpose: Allows you to create "presets" for text form inputs. "Lightweight and simple to setup, it adds a lot of convenience for ~1kb of code." Only one function, two parameters: First...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.