I have an string array ["[1,3]","[1,5]"] how to convert it to [[1,2],[1,5]]?
CodePudding user response:
explore ast package in library, but be careful and know what you are exactly manipulating, because if not used properly you will open security holes in your application your building.
CodePudding user response:
You could use a list comprehension to evaluate the strings.
array = [eval(a) for a in array]
As mentioned elsewhere, this is a very risky if your input is unsanitised as it runs code that you may not have written (more info here) that may exploit your system or cause random errors so if you can find another way that would be optimal.
CodePudding user response:
You could also use json.loads()
for every element in the list.
import json
lst = ["[1,3]","[1,5]"]
output = []
for i in lst:
output.append(json.loads(i))
print(output)
Output
[[1, 3], [1, 5]]