I have a ndarray with string entries. I also have a second array which contains every string entry of the ndarray uniquely. So i want to replace the strings of the ndarray by the position of the second array, where the string are defined. I have tried this:
import scipy as sp
extern_nodes = sp.array([['B11', 'B6', '-1', '-1', '-1', '-1'],
['B9', 'B3', '-1', '-1', '-1', '-1'],
['B10', 'B5', '-1', '-1', '-1', '-1'],
['B8', 'B2', '-1', '-1', '-1', '-1'],
['B16', 'B6', '-1', '-1', '-1', '-1'],
['B15', 'B5', '-1', '-1', '-1', '-1'],
['B14', 'B3', '-1', '-1', '-1', '-1'],
['B12', 'B1', '-1', '-1', '-1', '-1'],
['B13', 'B2', '-1', '-1', '-1', '-1']], dtype='<U6')
nodes_sorted = sp.array(['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B8', 'B9', 'B10', 'B11',
'B12', 'B13', 'B14', 'B15', 'B16'], dtype='<U3')
for i in range(0, len(nodes_sorted)):
extern_nodes = sp.char.replace(extern_nodes, nodes_sorted[i], str(i))
And I'm getting as result
Out:extern_nodes: array([['01', '5', '-1', '-1', '-1', '-1'],
['7', '2', '-1', '-1', '-1', '-1'],
['00', '4', '-1', '-1', '-1', '-1'],
['6', '1', '-1', '-1', '-1', '-1'],
['06', '5', '-1', '-1', '-1', '-1'],
['05', '4', '-1', '-1', '-1', '-1'],
['04', '2', '-1', '-1', '-1', '-1'],
['02', '0', '-1', '-1', '-1', '-1'],
['03', '1', '-1', '-1', '-1', '-1']], dtype='<U2')
Which means the entries "B1X" were replaced in the first step by "0X" because of replacing "B1" by "0".
I couldn't find a way to specify exactly matching replacements of strings. My aim is to get in the first step of the for-loop this ndarray (only replacing every "B1" by "0" without replacing other "XB1X" strings..)
extern_nodes = sp.array([['B11', 'B6', '-1', '-1', '-1', '-1'],
['B9', 'B3', '-1', '-1', '-1', '-1'],
['B10', 'B5', '-1', '-1', '-1', '-1'],
['B8', 'B2', '-1', '-1', '-1', '-1'],
['B16', 'B6', '-1', '-1', '-1', '-1'],
['B15', 'B5', '-1', '-1', '-1', '-1'],
['B14', 'B3', '-1', '-1', '-1', '-1'],
['B12', '0', '-1', '-1', '-1', '-1'],
['B13', 'B2', '-1', '-1', '-1', '-1']], dtype='<U6')