Numpy字符串操作 |替换()函数
在numpy.core.defchararray.replace()函数中,arr 中的每个元素返回字符串的副本,其中所有出现的子字符串 old 都替换为 new。
Syntax : numpy.core.defchararray.replace(arr, old, new, count = None)
Parameters :
arr : [array-like of str] Given array-like of string.
old : [str or unicode] Old substring you want to replace.
new : [str or unicode] New substring which would replace the old substring.
count : [int, optional] If the optional argument count is given, only the first count occurrences are replaced.
Return : [ndarray] Return the output array of str .
代码#1:
Python3
# Python program explaining
# numpy.char.replace() function
# importing numpy as geek
import numpy as geek
gfg = geek.char.replace('GFG | a computer science portal for geeks', 'GFG', 'GeeksforGeeks')
print (gfg)
Python3
# Python program explaining
# numpy.char.replace() function
# importing numpy as geek
import numpy as geek
gfg = geek.char.replace('This is a python article', 'python', 'Numpy-Python', count = 1)
print (gfg)
输出 :
GeeksforGeeks | a computer science portal for geeks
代码#2:
Python3
# Python program explaining
# numpy.char.replace() function
# importing numpy as geek
import numpy as geek
gfg = geek.char.replace('This is a python article', 'python', 'Numpy-Python', count = 1)
print (gfg)
输出 :
This is a Numpy-Python article