numpy.char.replace#
- char.replace(a, old, new, count=-1)[源代码]#
对于
a中的每个元素,返回将子字符串old替换为new的字符串副本.- 参数:
- a : array_like, 具有
bytes_或str_dtypearray_like, withbytes_orstr_dtype - old, new : array_like, 具有
bytes_或str_dtypearray_like, withbytes_orstr_dtype - count : array_like, 具有
int_dtypearray_like, withint_dtype 如果给出了可选参数
count,则仅替换前count个匹配项.
- a : array_like, 具有
- 返回:
- outndarray
StringDType,bytes_或str_dtype 的输出数组,具体取决于输入类型
参见
示例
>>> 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')