In Python, I currently have a one element list of elements given like so:
x= ['1.1,1.2,1.6,1.7']
where each of the values are only separated by commas. I want to make this a list of floats, e.g like
x=[1.1, 1.2, 1.6, 1.7]
I've tried x=[float(i) for i in x]
and x=[float(i) for i in x.split()]
, but both return errors.
x
is a list with one string, so to access that string you need x[0]
. That string is comma-separated, so you need to specify the delimiter: split(',')
. (Otherwise, split()
tries to split a string on whitespace, as described in the docs.)
So you end up with:
[float(i) for i in x[0].split(',')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With