Home > Mobile >  Python List Gives an Error of Numpy Array?
Python List Gives an Error of Numpy Array?

Time:11-23

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]  

CodePudding user response:

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]]
  • Related