numpy.strings.replace#

strings.replace(a, old, new, count=-1)[原始碼]#

對於 a 中的每個元素,傳回一個字串副本,其中的子字串 old 會被 new 取代。

參數:
aarray_like,具有 bytes_str_ dtype
old, newarray_like,具有 bytes_str_ dtype
countarray_like,具有 int_ dtype

如果給定了選用參數 count,則只會取代前 count 個出現次數。

傳回值:
outndarray

StringDTypebytes_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')