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
I think you need to change the dtype
of the array, see eg 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 (eg 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)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.