3

i have a 2d array of strings and i want to replace them with other strings that are bigger in length. I tried this

for key, value in UniqueIds.items():
            indices[indices[...] == str(value)] = key

to replace each value with the corresponding key, but each value is 4 bytes and the key is about 10, and the changed value shows only the first 4 letters

christk
  • 834
  • 11
  • 23

2 Answers2

2

I think you need to change the dtype of the array, see e.g. here or also here. A 4-character string would be dtype='<U4'. If you'd have an 8-character string, it would be dtype='<U8' and so on.

So if you know the size of your resulting strings, you could specify it explicitly (e.g.dtype='<U10' to hold 10 Unicode characters). If you don't care about memory and copy operations, make it dynamic by using object as dtype:

import numpy as np
s = np.array(['test'], dtype=object)
s[0] = 'testtesttesttest'
# s
# array(['testtesttesttest'], dtype=object)

now .replace() will work:

s[0] = s[0].replace('test', 'notatest')
# s
# array(['notatestnotatestnotatestnotatest'], dtype=object)
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
0

the problem was that i converted the initial array of ints to an array of strings like this :

indices = np.char.mod('%d', indices)

When i changed the line above with this one:

indices = indices.astype(str)

everything worked as expected.

christk
  • 834
  • 11
  • 23
  • ok ;-) still... check the `dtype` of `indices` - the resulting `dtype` will depend on what you had before calling `.astype(str)`. If it e.g. was `int32`, `dtype` is something like ` – FObersteiner Aug 05 '19 at 16:07
  • Alright, thanks and I hope it will be helpful anyway. – FObersteiner Aug 05 '19 at 16:12