Denis
Denis

Reputation: 151

Converting Numpy ndarray to a list

This is the result I get:

a=[array([[ 0.05019716]]), array([[ 0.04085874]])]

I would like to create such a list:

list_numbers=[ 0.05019716, 0.04085874]

Any advice will be appreciated

Upvotes: 2

Views: 2628

Answers (1)

Zero
Zero

Reputation: 76917

Here's one way

In [9]: np.array(a).ravel().tolist()
Out[9]: [0.05019716, 0.04085874]

Convert a, list, to NumPy array, then flatten it to array elements using ravel(), then convert to list using tolist().

However, for this specific case, you could also use list comprehensions

In [10]: [x[0][0] for x in a]   # or x[0, 0]
Out[10]: [0.050197159999999998, 0.040858739999999998]

Upvotes: 2

Related Questions