Ad

How Do I Convert Split String To An Array Of Numbers?

- 1 answer

I have a string that need to be spliced based on ,

x = '1,0.5,3'
y = x.split(',')
print(y)

//Result
//['1','0.5','3']

I would like to split the string but get an array of numbers as return value.

expected return

[1,0.5,3]
Ad

Answer

x = '1,0.5,3'
l = [float(a) for a in x.split(',')]

Result:

[1,0.5,3]

Used float() since you have a floating point in there. You can use int() but that will do some rounding

Ad
source: stackoverflow.com
Ad