Skip to content Skip to sidebar Skip to footer

Remove String Quotes From Array In Python

I'm trying to get rid of some characters in my array so I'm just left with the x and y coordinates, separated by a comma as follows: [[316705.77017187304,790526.7469308273] [32173

Solution 1:

Using 31.2. ast — Abstract Syntax Trees¶

importastxll=  [['321731.20991025254,''790958.3493565321,'], ['321731.20991025254,''790958.3493565321,']]
>>> [ast.literal_eval(xl[0]) for xl in xll]
[(321731.20991025254, 790958.3493565321), (321731.20991025254, 790958.3493565321)]

Above gives list of tuples for list of list, type following:

>>> [list(ast.literal_eval(xl[0])) for xl in xll]
[[321731.20991025254, 790958.3493565321], [321731.20991025254, 790958.3493565321]]

OLD: I think this:

>>> sll
[['316705.770172', '790526.746931'], ['321731.20991', '790958.349357']]
>>> fll = [[float(i) for i in l] for l in sll]
>>> fll
[[316705.770172, 790526.746931], [321731.20991, 790958.349357]]
>>> 

old Edit:

>>>xll =  [['321731.20991025254,''790958.3493565321,'], ['321731.20991025254,''790958.3493565321,']]>>>[[float(s) for s in xl[0].split(',') if s.strip() != ''] for xl in xll]
[[321731.20991025254, 790958.3493565321], [321731.20991025254, 790958.3493565321]]

Post a Comment for "Remove String Quotes From Array In Python"