python - Turning a string into list of positive and negative numbers -


if have string in form 'a,b,c,d,e' letters positive or negative numbers, e.g '1,-2,3,4,-5' , want turn tuple e.g (1,-2,3,4,-5), how this?

one method use ast.literal_eval:

safely evaluate expression node or string containing python expression. string or node provided may consist of following python literal structures: strings, numbers, tuples, lists, dicts, booleans, , none.

this can used safely evaluating strings containing python expressions untrusted sources without need parse values oneself.

>>> import ast >>> ast.literal_eval('1,-2,3,4,-5') (1, -2, 3, 4, -5) 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -