Answer by juliomalegria for converting string to tuple
In this case, the ast module could be useful:>>> from ast import literal_eval>>> s = '(1,2,3,4,5),(5,4,3,2,1)'>>> my_tuples = literal_eval(s)>>> my_tuples((1, 2, 3,...
View ArticleAnswer by tito for converting string to tuple
You can use ast.literal_eval():Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures:...
View ArticleAnswer by ovgolovin for converting string to tuple
You may use eval. I think it'll be the shortest one.>>> s = '(1,2,3,4,5),(5,4,3,2,1)'>>> ts = eval(s)>>> ts((1, 2, 3, 4, 5), (5, 4, 3, 2, 1))>>> tsp =...
View Articleconverting string to tuple
I need to write a function that takes a string '(1,2,3,4,5),(5,4,3,2,1)' and returns a list of tuples of the 1st and last element of each tuple, [(1,5),(5,1)].I was thinking:def f(givenstring):...
View Article