替换 N-d numpy 数组中的字符串
replace strings in N-d numpy array
我有一个二维字符串数组,我想用其他长度更大的字符串替换它们。
我试过这个
for key, value in UniqueIds.items():
indices[indices[...] == str(value)] = key
将每个value替换成对应的key,但是每个value是4个字节,key大约是10个,修改后的value只显示前4个字母
我认为您需要更改数组的 dtype
,例如参见here or also here。一个 4 个字符的字符串将是 dtype='<U4'
。如果你有一个 8 个字符的字符串,它将是 dtype='<U8'
等等。
因此,如果您知道结果字符串的大小,则可以明确指定它(例如,dtype='<U10'
以容纳 10 个 Unicode 字符)。如果您不关心内存和复制操作,请使用 object
as dtype
:
使其动态化
import numpy as np
s = np.array(['test'], dtype=object)
s[0] = 'testtesttesttest'
# s
# array(['testtesttesttest'], dtype=object)
现在 .replace()
可以工作:
s[0] = s[0].replace('test', 'notatest')
# s
# array(['notatestnotatestnotatestnotatest'], dtype=object)
问题是我将初始的整数数组转换为字符串数组,如下所示:
indices = np.char.mod('%d', indices)
当我用这行更改上面的行时:
indices = indices.astype(str)
一切如预期。
我有一个二维字符串数组,我想用其他长度更大的字符串替换它们。 我试过这个
for key, value in UniqueIds.items():
indices[indices[...] == str(value)] = key
将每个value替换成对应的key,但是每个value是4个字节,key大约是10个,修改后的value只显示前4个字母
我认为您需要更改数组的 dtype
,例如参见here or also here。一个 4 个字符的字符串将是 dtype='<U4'
。如果你有一个 8 个字符的字符串,它将是 dtype='<U8'
等等。
因此,如果您知道结果字符串的大小,则可以明确指定它(例如,dtype='<U10'
以容纳 10 个 Unicode 字符)。如果您不关心内存和复制操作,请使用 object
as dtype
:
import numpy as np
s = np.array(['test'], dtype=object)
s[0] = 'testtesttesttest'
# s
# array(['testtesttesttest'], dtype=object)
现在 .replace()
可以工作:
s[0] = s[0].replace('test', 'notatest')
# s
# array(['notatestnotatestnotatestnotatest'], dtype=object)
问题是我将初始的整数数组转换为字符串数组,如下所示:
indices = np.char.mod('%d', indices)
当我用这行更改上面的行时:
indices = indices.astype(str)
一切如预期。