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
個出現次數。
- aarray_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')