Home > Software design >  how to covert ndarray object of numpy module to string in python
how to covert ndarray object of numpy module to string in python

Time:11-11

I have ndarray object of modules and i want to covert it to a string. for example i want to go from:

[[array(['img_10.jpg'], dtype='<U22')]
 [array(['img_11.jpg'], dtype='<U22')]
 [array(['img_12.jpg'], dtype='<U22')]
 [array(['img_13.jpg'], dtype='<U22')]]

to only a list ['img_10.jpg','img_11.jpg','img_12.jpg','img_13.jpg']

how can i do that to be able to read the images with its names??

More details:

enter image description here

showing the content of array gives:

enter image description here

after trying :

python_list = [k for a in lst for k in a]

enter image description here

i'm still having a list of type ndarray

CodePudding user response:

You could just use a simple comprehension, because numpy arrays are iterables. Assuming that your list is arr:

python_list = [k for a in arr for k in a]

Above assumed a simple array. Per your edit, you have a top 2D array of shaped (2338, 1). In that case, you will have to unpack another level:

python_list = [k for a in arr for b in a for k in b]
  • Related