Say I have the following array:
import numpy as np
a = ['hello','snake','plate']
I want this to turn into a numpy array b so that:
b[0,0] = 'h'
b[0,1] = 'e'
b[0,2] = 'l'
b[1,0] = 's'
...
I want the standard numpy tricks to work, like broadcasting, comparison, etc.
How is it done? And where is this in the numpy documentation?
Thanks!
Uri
You can create a numpy character array directly e.g.:
The usual array tricks work with this.
If you have
aand wish to generate b from it, note that:So you can do something like:
However, if
ahas words of unequal length (e.g.['snakes','on','a','plane']), what do you want to do with the shorter words? You could pad them with spaces to the longest word:Which the
string.center(width)pads with spaces, centering the string. You could also userjustorljust(see string docs).