numpy.strings.replace#
- strings.replace(a, old, new, count=-1)[源代码]#
对于
a中的每个元素,返回字符串的副本,并将子字符串old的出现替换为new.- 参数:
- a : array_like,具有
bytes_或str_dtypearray_like, 具有 - old, new : array_like,具有
bytes_或str_dtypearray_like, 具有 - count : array_like,具有
int_dtypearray_like, 具有 如果给出了可选参数
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')