Converting a list with numpy arrays into lists in Python
NickName:jcbose123 Ask DateTime:2023-01-02T16:34:38

Converting a list with numpy arrays into lists in Python

I have a list B11 containing a list of numpy arrays. I want to convert each of these arrays into lists but I am getting an error. I also show the expected output.

import numpy as np

B11=[[np.array([353.856161,   0.      ,   0.      ]), 
      np.array([  0.      ,   0.      , 282.754301,   0.      ])], 
     [np.array([  0.      , 294.983702, 126.991664])]]

C11=B11.tolist()

The error is

in <module>
    C11=B11.tolist()

AttributeError: 'list' object has no attribute 'tolist'

The expected output is

[[[353.856161,   0.      ,   0.      ],[  0.      ,   0.      , 282.754301,   0.      ]],
 [  0.      , 294.983702, 126.991664]]

Copyright Notice:Content Author:「jcbose123」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74980324/converting-a-list-with-numpy-arrays-into-lists-in-python

Answers
God Is One 2023-01-02T08:42:36

for x in B11:\n for y in x:\n print(y.tolist())\n\n#output:\n\n[353.856161, 0.0, 0.0]\n[0.0, 0.0, 282.754301, 0.0]\n[0.0, 294.983702, 126.991664]\n\nOr List comprehension to keep the values:\n[[y.tolist() for y in x] for x in B11]\n\n#[[[353.856161, 0.0, 0.0], [0.0, 0.0, 282.754301, 0.0]],\n#[[0.0, 294.983702, 126.991664]]]\n",


More about “Converting a list with numpy arrays into lists in Python” related questions

Converting a list with numpy arrays into lists in Python

I have a list B11 containing a list of numpy arrays. I want to convert each of these arrays into lists but I am getting an error. I also show the expected output. import numpy as np B11=[[np.array...

Show Detail

What are the downsides of always using numpy arrays instead of python lists?

I'm writing a program in which I want to flatten an array, so I used the following code: list_of_lists = [["a","b","c"], ["d","e","f"], ["g","h","i"]]&

Show Detail

Converting list of arrays into list of lists in Python

I have a list I which contains numpy arrays. I want to convert these arrays into lists as shown in the current and expected outputs. import numpy as np I=[np.array([[0, 1], [0, 2], ...

Show Detail

Removing duplicates from a list of numPy arrays

I have an ordinary Python list that contains (multidimensional) numPy arrays, all of the same shape and with the same number of values. Some of the arrays in the list are duplicates of earlier ones...

Show Detail

Converting numpy array which elements are numpy arrays to a list of numpy arrays

I have a 4D numpy array which each element is a numpy array itself. I want to convert it to a list of numpy arrays. I saw tolist() but it is also converting each element to a list as well. My numpy...

Show Detail

Improve performance of converting matlab arrays to numpy arrays

I stumbled upon that question. I'm using python from matlab (reference). creating numpy arrays is as easy as py.numpy.array(rand(1, 10)). By passing the dimensions I can also create a small method...

Show Detail

Unexpected result in an efficiency experiment between python lists vs numpy arrays

I'm doing some experiments in order to compare the efficiency of python lists and numpy arrays. Afaik, numpy arrays are more efficient than lists. However, the following code gives surprising resul...

Show Detail

Can NumPy arrays and lists be modified in Python functions?

Do NumPy arrays and lists get modified in Python functions? If yes, then how to take care that the values are not modified?

Show Detail

Python code quality aspects list comprehension versus numpy arrays

I am doing an online course on machine learning. To get better predictions we should implement a python function to remove 10% of the outlier data. I have solved the quest - yet I have found two

Show Detail

memory usage: numpy-arrays vs python-lists

Numpy is known for optimized arrays and various advantages over python-lists. But when I check for the memory usage python-lists have less space than the numpy arrays. The code I used is entered be...

Show Detail