Discussion:
[Tutor] split a string inside a list
Kayla Hiltermann
2015-05-09 03:24:48 UTC
Permalink
hi,

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. 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. 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.

sorry if that was super confusing. below is my code. there is a commented out section after sides = raw_input … that i left in for reference. all other parts of the code work. any suggestions would be greatly appreciated!! thanks



import re

def pythagorean_function():
sides = raw_input("Please enter three sides to a triangle: \n").split(" |,")

'''
when sides is created, it is a string. when it is split by a .split action,
it becomes a list regardless if the items in sides are actually split or not.
i can't figure out how to split sides (when created) by a comma OR a space.
the following code attempts (and fails).

re.split(r",| ",sides)
print sides

for item in sides:
if "," in item:
re.split(r",",item)
print "comma"
'''

sides_int = []
for value in sides:
try:
sides_int.append(int(value))
except ValueError:
continue

while len(sides_int) != 3:
print ("you did not enter THREE sides! remember all sides must be integers \n")
break



sides.sort()

if sides[0]**2 + sides[1]**2 == sides[2]**2:
print "\nthis triangle IS a pythagorean triple!\n"
else:
print "\nthis triangle is NOT a pythagorean triple\n"

redo()

def redo():
redo_question = raw_input("would you like to see if another triangle is a pythagorean triple? Y/N\n")
if redo_question == "Y":
pythagorean_function()
else:
print "thanks for stopping by!"

pythagorean_function()
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/t
Alan Gauld
2015-05-09 08:13:35 UTC
Permalink
Post by Kayla Hiltermann
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,
I would first replace all non-space separators with spaces
then do the split

separators = ',.;:' # plus any others you might get
for sep in separators:
inString.replace(sep,' ')
sides = inString.split()
Post by Kayla Hiltermann
import re
sides = raw_input("Please enter three sides to a triangle: \n").split(" |,")
sides_int = []
sides_int.append(int(value))
continue
You could use a list comprehension here, although you
may not have seen them yet. It would look like:

try:
sides_int = [int(value) for value in sides]
except ValueError:
print ("Remember all sides must be integers \n")
return # no point continuing with the function using bad data
Post by Kayla Hiltermann
print ("you did not enter THREE sides! remember all sides must be integers \n")
break
This should just be an 'if' test, not a while loop.
You only want to test the length once.
Post by Kayla Hiltermann
sides.sort()
print "\nthis triangle IS a pythagorean triple!\n"
print "\nthis triangle is NOT a pythagorean triple\n"
redo()
Rather than use recursion here it would be better to
put your function in a top level while loop.
Post by Kayla Hiltermann
redo_question = raw_input("would you like to see if another triangle is a pythagorean triple? Y/N\n")
pythagorean_function()
print "thanks for stopping by!"
You could write that like

redo_question = 'Y'
while redo_question.upper() == 'Y':
pythagorean_function()
redo_question = raw_input("would you like to see if another
triangle is a pythagorean triple? Y/N\n")

print "thanks for stopping by!"


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Dave Angel
2015-05-09 11:00:10 UTC
Permalink
Post by Alan Gauld
Post by Kayla Hiltermann
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,
I would first replace all non-space separators with spaces
then do the split
separators = ',.;:' # plus any others you might get
inString.replace(sep,' ')
inString = inString.replace(sep,' ')
Post by Alan Gauld
sides = inString.split()
Post by Kayla Hiltermann
import re
sides = raw_input("Please enter three sides to a triangle: \n").split(" |,")
sides_int = []
sides_int.append(int(value))
continue
You could use a list comprehension here, although you
sides_int = [int(value) for value in sides]
print ("Remember all sides must be integers \n")
return # no point continuing with the function using bad data
Post by Kayla Hiltermann
print ("you did not enter THREE sides! remember all sides must be integers \n")
break
This should just be an 'if' test, not a while loop.
You only want to test the length once.
Post by Kayla Hiltermann
sides.sort()
print "\nthis triangle IS a pythagorean triple!\n"
print "\nthis triangle is NOT a pythagorean triple\n"
redo()
Rather than use recursion here it would be better to
put your function in a top level while loop.
Post by Kayla Hiltermann
redo_question = raw_input("would you like to see if another
triangle is a pythagorean triple? Y/N\n")
pythagorean_function()
print "thanks for stopping by!"
You could write that like
redo_question = 'Y'
pythagorean_function()
redo_question = raw_input("would you like to see if another
triangle is a pythagorean triple? Y/N\n")
print "thanks for stopping by!"
HTH
--
DaveA
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-05-09 08:19:16 UTC
Permalink
Post by Kayla Hiltermann
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
... return s.replace(",", " ").split()
...
Post by Kayla Hiltermann
my_split("1 2 3")
['1', '2', '3']
Post by Kayla Hiltermann
my_split("1,2,3")
['1', '2', '3']
Post by Kayla Hiltermann
my_split("1, 2, 3")
['1', '2', '3']
Post by Kayla Hiltermann
my_split("1, 2, 3,,,")
['1', '2', '3']

When you know it it's easy :)
Post by Kayla Hiltermann
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
Post by Kayla Hiltermann
import re
re.split(" |,", "1 2 3")
['1', '2', '3']
Post by Kayla Hiltermann
re.split(" |,", "1,2,3")
['1', '2', '3']
Post by Kayla Hiltermann
re.split(" |,", "1, 2, 3")
['1', '', '2', '', '3']
Post by Kayla Hiltermann
re.split(r"\s*[,\s]\s*", "1, 2 , 3")
['1', '2', '3']
Post by Kayla Hiltermann
re.findall(r"\d+", "1, 2 , 3")
['1', '2', '3']

But I recommend that you stick with the str.split() approach shown first.
Post by Kayla Hiltermann
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.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.o
Alex Kleider
2015-05-09 15:17:17 UTC
Permalink
Post by Kayla Hiltermann
hi,
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,
3 4 5
3,4,5
3, 4, 5
all become: [“3", “4", “5"].
Several solutions have been suggested (of which the re.findall approach
appeals to me the most) but one that hasn't and might be worth
considering is use of str.maketrans() and s.translate().
https://docs.python.org/2/library/string.html#string.maketrans
https://docs.python.org/2/library/string.html#string.translate

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.pytho
Mark Lawrence
2015-05-09 15:32:09 UTC
Permalink
Post by Alex Kleider
Post by Kayla Hiltermann
hi,
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,
3 4 5
3,4,5
3, 4, 5
all become: [“3", “4", “5"].
Several solutions have been suggested (of which the re.findall approach
appeals to me the most) but one that hasn't and might be worth
considering is use of str.maketrans() and s.translate().
https://docs.python.org/2/library/string.html#string.maketrans
https://docs.python.org/2/library/string.html#string.translate
From https://docs.python.org/3/whatsnew/3.1.html#other-language-changes

"The string.maketrans() function is deprecated and is replaced by new
static methods, bytes.maketrans() and bytearray.maketrans(). This change
solves the confusion around which types were supported by the string
module. Now, str, bytes, and bytearray each have their own maketrans and
translate methods with intermediate translation tables of the
appropriate type."
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://m
Loading...