0

I am trying to convert the data type of a list into float. I know how to convert the data type of each list using for-loop, however, I really don't know how to convert the data type of each item of a list, i.e., I have an array with the data type string such that

array(['5, 0, -150, 0', '6, 0, -10, 0',
       '7, 2.5881904510252, 9.6592582628907, 0',
       '8, 5, 8.6602540378444, 0',
       '9, 7.0710678118655, 7.0710678118655, 0',
       '10, 8.6602540378444, 5, 0'], dtype='<U63')

then, how can I build two dimensional array as 6x4 array of float data type?

2
  • If this is related to numpy add the appropriate tag. Commented Dec 30, 2019 at 9:05
  • 1
    use np.asarray([[float(y) for y in x.split(',')] for x in your_array]) Commented Dec 30, 2019 at 9:09

2 Answers 2

3

Iterate on that array, split those strings on delimiter, then make them of float datatype.

>>> arr2 = np.array([np.array([float(i.strip()) for i in j.split(',') if i]) for j in arr1])
>>> arr2
array([[   5.        ,    0.        , -150.        ,    0.        ],
       [   6.        ,    0.        ,  -10.        ,    0.        ],
       [   7.        ,    2.58819045,    9.65925826,    0.        ],
       [   8.        ,    5.        ,    8.66025404,    0.        ],
       [   9.        ,    7.07106781,    7.07106781,    0.        ],
       [  10.        ,    8.66025404,    5.        ,    0.        ]])
>>> arr2.dtype
dtype('float64')
Sign up to request clarification or add additional context in comments.

1 Comment

You give the outer np.array a dtype=float parameter, you don't need the inner float() iteration.
0
In [72]: arr = np.array(['5, 0, -150, 0', '6, 0, -10, 0', 
    ...:        '7, 2.5881904510252, 9.6592582628907, 0', 
    ...:        '8, 5, 8.6602540378444, 0', 
    ...:        '9, 7.0710678118655, 7.0710678118655, 0', 
    ...:        '10, 8.6602540378444, 5, 0'], dtype='<U63')                     

The list comprehension that the others propose is the right way, but it can be simplified:

In [73]: [line.split(',') for line in arr]                                      
Out[73]: 
[['5', ' 0', ' -150', ' 0'],
 ['6', ' 0', ' -10', ' 0'],
 ['7', ' 2.5881904510252', ' 9.6592582628907', ' 0'],
 ['8', ' 5', ' 8.6602540378444', ' 0'],
 ['9', ' 7.0710678118655', ' 7.0710678118655', ' 0'],
 ['10', ' 8.6602540378444', ' 5', ' 0']]

np.array can take care of handling the nested lists, and conversion to float:

In [74]: np.array(_, dtype=float)                                                     
Out[74]: 
array([[   5.        ,    0.        , -150.        ,    0.        ],
       [   6.        ,    0.        ,  -10.        ,    0.        ],
       [   7.        ,    2.58819045,    9.65925826,    0.        ],
       [   8.        ,    5.        ,    8.66025404,    0.        ],
       [   9.        ,    7.07106781,    7.07106781,    0.        ],
       [  10.        ,    8.66025404,    5.        ,    0.        ]])

The fact that the original object is an array rather than a list doesn't enhance this conversion. In fact iterating on the array is slower than iterating on the equivalent list.

Comments

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.