I am learning Python 3. One thing I am doing to get some repetition is going through checkio. It is a website with coding exercises. One of the exercises gives an input string as follows:
str_to_nbrs("(2,2),(6,2),(2,6)")
str_to_nbrs("(3,7),(6,10),(9,7)")
I need the output of this code to produce a list of sub lists with the pairs of numbers so I can put them into variables to use for calculations.
This exercise actually has to do with x and y coordinates and circles. I am not sure why this input was in the form of a string. But, it gives me a chance to learn how to use the tools to convert a string like this to numbers that I can use in mathematical processes.
I figured out how to accomplish what I need. I am relatively new to Python 3. But, from what I have seen already, it is a well-thought-out and efficient language. I would bet there is a better way to do it with less code.
Please note that I added the big obvious comments to explain what the code is doing. They are not part of my regular coding style.
One more thing: I am still trying to come up with a convention for variable names. I have started lists with "l_". I try to give things meaningful names, but also try to keep them relatively short. I know "l_work" isn't a good name. But, I just wanted to finish that section of code, and used that...err...unimaginative name.
import re
# str_to_nbrs("(2,2),(6,2),(2,6)")
def str_to_nbrs(data):
print("String input:")
print(data)
print()
new_data = re.sub(r"\(","",data)
new_data = new_data + ","
print("Remove left parenthesis and add a comma a the end for split method:")
print(new_data)
print()
l_data = new_data.split("),")
l_data.pop()
print("Reformatted string data in a list:")
print(l_data)
print()
l_work = []
l_nbrs = []
l_all = []
for i in l_data:
l_work = i.split(",")
for j in l_work:
l_nbrs.append(int(j))
l_all.append(l_nbrs)
l_nbrs = []
print("Integers in sub lists:")
print(l_all)
print()
print("---------------------")
print()
str_to_nbrs("(2,2),(6,2),(2,6)")
str_to_nbrs("(3,7),(6,10),(9,7)")