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 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
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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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...
|
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...
|
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:...
|
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,...
|
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...
|
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...
|
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
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |