numpy.char.replace#
- char.replace(a, old, new, count=-1)[源代码]#
对于
a中的每个元素,返回一个字符串副本,其中子字符串old被new替换。- 参数:
- a类似数组,具有
bytes_或str_dtype - old, new类似数组,具有
bytes_或str_dtype - count类似数组,具有
int_dtype 如果提供了可选参数
count,则仅替换前count次出现。
- a类似数组,具有
- 返回:
- 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')