numpy字符串操作 | isspace()函数
如果字符串中只有空白字符并且至少有一个字符, numpy.core.defchararray.isspace(arr)
函数对每个元素返回 true。否则返回 false。
Syntax: numpy.core.isspace(arr)
Parameters:
arr : array_like of str or unicode.
Returns : [ndarray] Output array of bools.
代码#1:
# Python program explaining
# numpy.char.isspace() method
import numpy as geek
# input arrays not containing any space
in_arr = geek.array([ 'Geeksforgeeks', 'Codechef'] )
print ("Input array : ", in_arr)
out_arr = geek.char.isspace(in_arr)
print ("Output array: ", out_arr)
输出:
Input array : ['Geeksforgeeks' 'Codechef']
Output array: [False False]
代码#2:
# Python program explaining
# numpy.char.isspace() method
import numpy as geek
# input arrays containing string along with space
in_arr = geek.array([ 'Geeks\nfor\ngeeks', 'Code\tchef'] )
print ("Input array : ", in_arr)
out_arr = geek.char.isspace(in_arr)
print ("Output array: ", out_arr)
输出:
Input array : ['Geeks\nfor\ngeeks' 'Code\tchef']
Output array: [False False]
代码#3:
# Python program explaining
# numpy.char.isspace() method
import numpy as geek
# input arrays containing only white space
in_arr = geek.array([ '\n', '\t', ' ', '\n\t '] )
print ("Input array : ", in_arr)
out_arr = geek.char.isspace(in_arr)
print ("Output array: ", out_arr)
输出:
Input array : ['\n' '\t' ' ' '\n\t ']
Output array: [ True True True True]