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]
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]
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
If you really need them to be ints instead of floats, you can truncate them for example:
>>> [int(float(e).__trunc__()) for e in x.split(",")]
[1, 0, 3]
You have to convert each item:
y = [float(y) for y in x.split(',')] // Result: [1.0, 0.5, 3.0]
Note: Using int(y)
directly gave me this error due to the "0.5":
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '0.5'