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 1196
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: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
| |