Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python List Gives an Error of Numpy Array?

I have a list which is 2 dimensional and has elements like ([0,1,2],[3,4,5]). Type of its elements is numpy.ndarray. I am trying to delete 2nd columns of each element. When I check its type, it returns list but it gives ValueError: cannot delete array elements error. I checked StackOverflow but haven’t found a similar case. The code is below, any help is appreciated.

for row in trainSet:
    del row[1]  

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Its a list that contains numpy arrays

import numpy as np
trainSet = np.array([[0,1,2],[3,4,5]]) 


#wrong way list(trainSet) # this do not convert nested lists
trainSet = list(map(lambda x: x.tolist(),trainSet)) #to make sure it is a list, do not contain nested numpy arrays
for row in trainSet:
    del row[1] 

print(trainSet)

[[0, 2], [3, 5]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading