[Tutor] split a string inside a list
Peter Otten
__peter__ at web.de
Sat May 9 10:19:16 CEST 2015
Kayla Hiltermann wrote:
> i am trying to make a pythagorean triples checker (a^2 + b^2 = c^2). the
> user enters three sides to a triangle and my code determines if it is a
> pythagorean triple (aka right triangle) or not. i have the entire code
> pretty much done, except i want to account for variability in user input,
> like using commas or just spaces. the user input is initially a string,
> but is converted to a list once run through .split() . I would like to
> split the user input by commas or spaces, so: 3 4 5 3,4,5 3, 4, 5 all
> become: [“3", “4", “5"]. yes, the inputs are strings but i convert - or
> make sure - they are integers later in the program.
Here's the trick: convert the commas to space, then use the string method to
split:
>>> def my_split(s):
... return s.replace(",", " ").split()
...
>>> my_split("1 2 3")
['1', '2', '3']
>>> my_split("1,2,3")
['1', '2', '3']
>>> my_split("1, 2, 3")
['1', '2', '3']
>>> my_split("1, 2, 3,,,")
['1', '2', '3']
When you know it it's easy :)
> my main issue is that
> i cannot split by commas or spaces at the same time. i tried using the
> vertical bar - .split(“ |,”) but it only executed the split by space, not
> the split by comma.
The str.split() method does not understand regular expressions. You need
re.split() for this:
>>> import re
>>> re.split(" |,", "1 2 3")
['1', '2', '3']
>>> re.split(" |,", "1,2,3")
['1', '2', '3']
>>> re.split(" |,", "1, 2, 3")
['1', '', '2', '', '3']
To accept a combination of one comma and whitespace:
>>> re.split(r"\s*[,\s]\s*", "1, 2 , 3")
['1', '2', '3']
Or just look for digits:
>>> re.findall(r"\d+", "1, 2 , 3")
['1', '2', '3']
But I recommend that you stick with the str.split() approach shown first.
> as of now, i can only split by either. the issue is
> that when i split by spaces - .split(“ ) , “3,4,5” does not split and
> becomes the list [“3,4,5”]. on the other hand, “3, 4, 5” does split, but
> becomes [“3,”, “4,”, “5”]. the problem is the same if i replace the
> .split(“ “) by split(“,”), only the commas are replaced by spaces.
More information about the Tutor
mailing list