Home > Back-end >  I want to remove ' from numpy
I want to remove ' from numpy

Time:11-17

I want to remove the value of '' while creating the numpy array In the following situation, how can I remove the quotation mark that comes out by multiplying the 'character' by 0 and leave only 'character'?

import numpy as np

array = np.array(['character'*1,'character'*0])

Expected array(['character'], dtype='<U9')

np.delete(array ,"''")

IndexError: arrays used as indices must be of integer (or boolean) type

CodePudding user response:

You can solve this in different ways,

here is one example:

import numpy as np

array = np.array([ele for ele in ['character'*1,'character'*0] if len(ele) > 0])
# or
array = np.array([ele for ele in ['character'*1,'character'*0] if ele != ''])

And to get your method working:

array = np.delete(array, array=='')

EDIT

And for @S3DEV:

import numpy as np

array = np.array(['character'*1,'character'*0])
array = array[array != '']
  • Related