By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

Simple question to split

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/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?
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

P: n/a
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

P: n/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?
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 discussion thread is closed

Replies have been disabled for this discussion.