Numpy字符串操作 |分区()函数
在numpy.core.defchararray.partition()函数中,arr 中的每个元素,将元素拆分为第一次出现的 sep,并返回包含分隔符之前的部分、分隔符本身和分隔符之后的部分的 3 个字符串。如果没有找到分隔符,则返回包含字符串本身的 3 个字符串,后跟两个空字符串。
Syntax : numpy.core.defchararray.partition(arr, sep)
Parameters :
arr : [array_like, {str, unicode}] Given Input array.
sep : [str or unicode] Separator to split each string element in arr.
Return : [ndarray] Return the output array of str or unicode, depending on input type.
代码#1:
Python3
# Python program explaining
# numpy.char.partition() function
# importing numpy as geek
import numpy as geek
arr = "GeeksforGeeks - A computer science portal for geeks"
sep ='None'
gfg = geek.char.partition(arr, sep)
print (gfg)
Python3
# Python program explaining
# numpy.char.partition() function
# importing numpy as geek
import numpy as geek
arr = "GeeksforGeeks - A computer science portal for geeks"
sep = 'portal'
gfg = geek.char.partition(arr, sep)
print (gfg)
输出 :
[‘GeeksforGeeks – A computer science portal for geeks’ ” ”]
代码#2:
Python3
# Python program explaining
# numpy.char.partition() function
# importing numpy as geek
import numpy as geek
arr = "GeeksforGeeks - A computer science portal for geeks"
sep = 'portal'
gfg = geek.char.partition(arr, sep)
print (gfg)
输出 :
[‘GeeksforGeeks – A computer science ‘ ‘portal’ ‘ for geeks’]