numpy.strings.replace#

strings.replace(a, old, new, count=-1)[源代码]#

对于 a 中的每个元素,返回将子字符串 old 替换为 new 的字符串副本.

参数:
a : array_like, 具有 bytes_str_ dtypearray_like, with bytes_ or str_ dtype
old, new : array_like, 具有 bytes_str_ dtypearray_like, with bytes_ or str_ dtype
count : array_like, 具有 int_ dtypearray_like, with int_ dtype

如果给出了可选参数 count ,则仅替换前 count 个匹配项.

返回:
outndarray

StringDType , bytes_str_ dtype 的输出数组,具体取决于输入类型

参见

str.replace

示例

>>> import numpy as np
>>> a = np.array(["That is a mango", "Monkeys eat mangos"])
>>> np.strings.replace(a, 'mango', 'banana')
array(['That is a banana', 'Monkeys eat bananas'], dtype='<U19')
>>> a = np.array(["The dish is fresh", "This is it"])
>>> np.strings.replace(a, 'is', 'was')
array(['The dwash was fresh', 'Thwas was it'], dtype='<U19')