0

I'm trying to convert this numpy.ndarray to a list

[[105.53518731]
 [106.45317529]
 [107.37373843]
 [108.00632646]
 [108.56373502]
 [109.28813113]
 [109.75593207]
 [110.57458371]
 [111.47960639]]

I'm using this function to convert it.

conver = conver.tolist()

the output is this, I'm not sure whether it's a list and if so, can I access its elements by doing cover[0] , etc

[[105.5351873125], [106.45317529411764], [107.37373843478261], [108.00632645652173], [108.56373502040816], [109.28813113157895], [109.75593206666666], [110.57458370833334], [111.47960639393939]]

finally, after I convert it to a list, I try to multiply the list members by 1.05 and get this error!

TypeError: can't multiply sequence by non-int of type 'float'

4
  • 2
    That's a list. It looks exactly how I would expect the result to look. Commented May 9, 2020 at 2:02
  • then what's the difference between [[105.5351873125], [106.45317529411764] and [105.5351873125,106.45317529411764] and why i'm getting the error ? Commented May 9, 2020 at 2:05
  • [] this means a list. However, this one here means a list that has different lists inside itself [ [], [], [] ] Commented May 9, 2020 at 2:06
  • What were you expecting, and what actually happened? Even if this case that's not a problem, please provide some expected output anyways. Commented May 9, 2020 at 6:52

4 Answers 4

3

You start with a 2d array, with shape (n,1), like this:

In [342]: arr = np.random.rand(5,1)*100                                                                
In [343]: arr                                                                                          
Out[343]: 
array([[95.39049043],
       [19.09502087],
       [85.45215423],
       [94.77657561],
       [32.7869103 ]])

tolist produces a list - but it contains lists; each [] layer denotes a list. Notice that the [] nesting matches the array's:

In [344]: arr.tolist()                                                                                 
Out[344]: 
[[95.39049043424225],
 [19.095020872584335],
 [85.4521542296349],
 [94.77657561477125],
 [32.786910295446425]]

To get a number you have to index through each list layer:

In [345]: arr.tolist()[0]                                                                              
Out[345]: [95.39049043424225]
In [346]: arr.tolist()[0][0]                                                                           
Out[346]: 95.39049043424225
In [347]: arr.tolist()[0][0]*1.05                                                                      
Out[347]: 100.16001495595437

If you first turn the array into a 1d one, the list indexing is simpler:

In [348]: arr.ravel()                                                                                  
Out[348]: array([95.39049043, 19.09502087, 85.45215423, 94.77657561, 32.7869103 ])
In [349]: arr.ravel().tolist()                                                                         
Out[349]: 
[95.39049043424225,
 19.095020872584335,
 85.4521542296349,
 94.77657561477125,
 32.786910295446425]
In [350]: arr.ravel().tolist()[0]                                                                      
Out[350]: 95.39049043424225

But if your primary goal is to multiply the elements, doing with the array is simpler:

In [351]: arr * 1.05                                                                                   
Out[351]: 
array([[100.16001496],
       [ 20.04977192],
       [ 89.72476194],
       [ 99.5154044 ],
       [ 34.42625581]])

You can access elements of the array with:

In [352]: arr[0,0]                                                                                     
Out[352]: 95.39049043424225

But if you do need to iterate, the tolist() option is good to know. Iterating on lists is usually faster than iterating on an array. With an array you should try to use the fast whole-array methods.

Sign up to request clarification or add additional context in comments.

Comments

0

you convert to list of list, so you could not broadcast.

import numpy as np
x = [[105.53518731],
 [106.45317529],
 [107.37373843],
 [108.00632646],
 [108.56373502],
 [109.28813113],
 [109.75593207],
 [110.57458371],
 [111.47960639],]
x = np.hstack(x)
x * 1.05

array([110.81194668, 111.77583405, 112.74242535, 113.40664278,
       113.99192177, 114.75253769, 115.24372867, 116.1033129 ,
       117.05358671])

Comments

0

yes, it's a list, you can check the type of a variable:

type(a)

to multiply each element with 1.05 then run the code below:

x = [float(i[0]) * 1.05 for i in a]
print(x)

Comments

0

Try this:

import numpy as np

a = [[105.53518731],
 [106.45317529],
 [107.37373843],
 [108.00632646],
 [108.56373502],
 [109.28813113],
 [109.75593207],
 [110.57458371],
 [111.47960639]]

b = [elem[0] for elem in a]
b = np.array(b)

print(b*1.05)

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.