4

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.

2
  • 1
    try x[0].split(',')? Commented Dec 29, 2016 at 18:36
  • x is a list, so you would need to access it via x[0]. Also you should provide the delimiter to the split call, e.g.: x[0].split(',') Commented Dec 29, 2016 at 18:36

5 Answers 5

6

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(',')]
Sign up to request clarification or add additional context in comments.

Comments

2

You can use map() like this:

list(map(float, x[0].split(',')))

map() takes a function (float in our case) and an iterable ( in our case this list: x[0].split(',')). float function is called for each item of our list x[0].split(',')

It is equivalent to this list comprehension:

[float(item) for item in x[0].split(',')]

3 Comments

In Python3+, map does not return a list, it returns a map object, you will have to convert it to a list
@IronFist Python 3 is not tagged in this question!
Neither Python2 is, so you must generalize your answer, plus you are referring to Python3 in your answer (linked to Python3 docs.)
0

Split the string by commas, and construct a float from each item:

[ float(item) for item in '1.1,2.3,5.1'.split(',') ]

1 Comment

OP has a different input
0

Make sure you are applying your split() function on a string and not the single element list. Use x[0] to ensure that. Also, pass a separator , to the split() function.

x = [float(i) for i in x[0].split(',')]

Comments

0

x=[float(i) for i in x.split()] is almost correct, except for two things. For one, you're not passing in anything to the split() function, so it's not going to split anything in your string (it would split only on whitespace, which your string does not have). You want to split by commas, so you have to pass a comma to it, like x.split(',').

Second, seeing as x is defined by x= ['1.1,1.2,1.6,1.7'], which is a list containing a single string, you would have to refer to the string in the array with x[0]. The final code would look like this:

x = ['1.1,1.2,1.6,1.7']
floats = [float(i) for i in x[0].split(',')]
print(floats)

This outputs a list of floats: [1.1, 1.2, 1.6, 1.7]

If x were just a string, like x = '1.1,1.2,1.6,1.7', then you would simply use floats = [float(i) for i in x.split(',')].

4 Comments

Those are lists, not arrays: docs.python.org/2/library/array.html
@UnholySheep Thanks, I've corrected the terminology.
FYI "not going to split on anything" is not correct, see docs - no delimiter means it splits on whitespace instead.
@whrrgarbl Thank you, I'll make that change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.