Simple question to split

  • Thread starter Matthias Winterland
  • Start date
M

Matthias Winterland

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
 
D

Dan Lenski

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)
 
D

Diez B. Roggisch

Matthias said:
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
 
D

Diez B. Roggisch

Diez said:
Matthias said:
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
 
G

Georg Brandl

Diez said:
Diez said:
Matthias said:
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
 
R

Robert Kern

Matthias said:
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
 
F

faulkner

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(',')
 
J

John Machin

Matthias said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top