Lesson14——NumPy 字串函式之 Par3:字串資訊函式

反差萌er發表於2022-02-16

NumPy 教程目錄

1 字串資訊函式

1.1 numpy.char.count

  char.count(a, sub, start=0, end=None) 返回一個陣列,其中包含 [start, end] 範圍內子字串 sub 的非重疊出現次數。

Example:

c = np.array(['aAaAaA', '  aA  ', 'abBABba'])
print(c)
print(np.char.count(c, 'A'))
print(np.char.count(c, 'aA'))
print(np.char.count(c, 'A', start=1, end=4))
print(np.char.count(c, 'A', start=1, end=3))
"""
['aAaAaA' '  aA  ' 'abBABba']
[3 1 1]
[3 1 0]
[2 1 1]
[1 0 0]
"""

1.2 numpy.char.endswith

   char.endswith(a, suffix, start=0, end=None) 返回一個布林陣列,該陣列為 True,其中 a 中的字串元素以字尾結尾,否則為 False

Example:

print(np.char.endswith(['Blair','Jane'],'r'))
print(np.char.endswith(['Blair','Jane'],'r',start=1,end=2))
print(np.char.endswith(['Blair','Jane'],'r',start=3,end=5))
s = np.array(['foo', 'bar'])
print(np.char.endswith(s, 'ar'))
print(np.char.endswith(s, 'a', start=1, end=2))
"""
[ True False]
[False False]
[ True False]
[False  True]
[False  True]
"""

1.3 numpy.char.find

  char.find(*a*, *sub*, *start=0*, end=None) 對於每個元素,返回字串中找到子字串 sub 的最低索引。

Example:

s = ['Blair','Jane',"Lee"]
print(np.char.find(s,'J'))
print(np.char.find(s,'a'))
print(np.char.find(s,'e',start=0,end=2))
"""
[-1  0 -1]
[ 2  1 -1]
[-1 -1  1]
"""

相關方法:

  • numpy.char.rfind

  char.rfind(a, sub, start=0, end=None) 對於 a 中的每個元素,返回找到子字串 sub 的字串中的最高索引,使得 sub 包含在 [start, end] 中。

Example:

s = ['JBlaJir','JaJne',"Lee"]
print(np.char.rfind(s,'J'))
print(np.char.rfind(s,'a'))
print(np.char.rfind(s,'e',start=0,end=2))
"""
[ 4  2 -1]
[ 3  1 -1]
[-1 -1  1]
"""

1.4 numpy.char.index

  char.index(a, sub, start=0, end=None) 與 find 類似,但在未找到子字串時引發 ValueError

Example:

s = ['Blair','Jane',"Lee"]
print(np.char.index(s,'J'))
print(np.char.index(s,'a'))
print(np.char.index(s,'e',start=0,end=2))
"""
ValueError
ValueError
ValueError
"""

s = ['Blaie','Jane',"Lee"]
print(np.char.index(s,'e',start=0,end=2))
"""
ValueError
"""

s = ['Blaie','Jane',"Lee"]
print(np.char.index(s,'e',start=0,end=5))
"""
[4 3 1]
"""

相關方法:

  • numpy.char.rindex

  char.rindex(a, sub, start=0, end=None) 與 rfind 類似,但在未找到子字串 sub 時引發 ValueError

Example:

s = ['JBlaJire','JaJne',"JaLee"]
print(np.char.rindex(s,'J'))
print(np.char.rindex(s,'a'))
print(np.char.rindex(s,'e',start=0,end=2))
"""
[ 4  2 -1]
[ 3  1 -1]
ValueError 
"""

1.5 numpy.char.isalpha

  char.isalpha(a) 如果字串中的所有字元都是字母並且至少有一個字元,則為每個元素返回 true,否則返回 false

Example:

s = ['Blaie','1111',"Lee"]
print(np.char.isalpha(s))
s = ['Blaie1','反差萌',"Lee"]
print(np.char.isalpha(s))
s = ['Blaie1','#####',"Lee"]
print(np.char.isalpha(s))
"""
[ True False  True]
[False  True  True]
[False False  True]
"""

1.6 numpy.char.isalnum

  char.isalnum(a) 如果字串中的所有字元都是字母數字並且至少有一個字元,則為每個元素返回 true,否則返回 false

Example:

s = ['Blaie','1111',"Lee"]
print(np.char.isalnum(s))
s = ['Blaie1','反差萌',"Lee"]
print(np.char.isalnum(s))
s = ['Blaie1','#####',"Lee"]
print(np.char.isalnum(s))
"""
[ True  True  True]
[ True  True  True]
[ True False  True]
"""

1.7 numpy.char.isdecimal

  char.isdecimal(a) 對於每個元素,如果元素中只有十進位制字元,則返回 True

Example:

s = ['Blaie','1111',"Lee"]
print(np.char.isdecimal(s))
s = ['Blaie1','反差萌',"Lee"]
print(np.char.isdecimal(s))
s = ['Blaie1','#####',"Lee"]
print(np.char.isdecimal(s))
s = ['1.1111','1+2j',"Lee"]
print(np.char.isdecimal(s))
"""
[False  True False]
[False False False]
[False False False]
[ True False False]
"""

1.8 numpy.char.isdigit

  char.isdigit(a) 如果字串中的所有字元都是數字並且至少有一個字元,則為每個元素返回 true,否則返回 false

Example:

s = ['Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###']
print(np.char.isdigit(s))
"""
[False  True False False False False False]
"""

1.9 numpy.char.islower

  char.islower(a) 如果字串中的所有大小寫字元都是小寫並且至少有一個大小寫字元,則為每個元素返回 true,否則返回 false

Example:

s = ['Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###','abc','ABC','1+2j#']
print(np.char.islower(s))
"""
[False False False False  True False False  True False  True]
"""

1.10 numpy.char.isnumeric

  char.isnumeric(a) 對於每個元素,如果元素中只有數字字元,則返回 True

Example:

s = ['Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###','abc','ABC','1+2j#']
print(np.char.isnumeric(s))  #只有全是數字時返回True
"""
[False  True False False False False False False False False]
"""

1.11 numpy.char.isspace

  char.isspace(a) 如果字串中只有空白字元並且至少有一個字元,則為每個元素返回 true,否則返回 false

Example:

s = ['Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###','abc','ABC','1+2j#','',' ','\t']
print(np.char.isspace(s))  #只有全是空白字元且至少有一個時返回True
"""
[False False False False False False False False False False False  True  True]
"""

1.12 numpy.char.istitle

  char.istitle(a) 如果元素是一個標題字串並且至少有一個字元,則為每個元素返回 true,否則返回 false

Example:

s = ['T','Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###','abc','ABC','1+2j#','',' ','\t']
print(np.char.istitle(s))  #只有全是空白字元且至少有一個時返回True
"""
[ True  True False  True False False False False False False False False
 False False]
"""

1.13 numpy.char.isupper 

  char.isupper(a) 如果字串中的所有大小寫字元都是大寫並且至少有一個字元,則為每個元素返回 true,否則返回 false

Example:

s = ['T','Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###','abc','ABC','1+2j#','',' ']
print(np.char.isupper(s))  #只有全是空白字元且至少有一個時返回True
i = 0
for tmp in np.char.isupper(s):
    print('%-6s -> %-5s'%(s[i],tmp))
    i = i+1
"""
[ True False False False False False False False False  True False False
 False]
T      -> True 
Blaie  -> False
1111   -> False
Lee007 -> False
1.11   -> False
1+1j   -> False
反差萌    -> False
###    -> False
abc    -> False
ABC    -> True 
1+2j#  -> False
       -> False
       -> False
"""

1.14 numpy.char.startswith

  char.startswith(a, prefix, start=0, end=None) 返回一個布林陣列,該陣列為 True,其中 a 中的字串元素以字首開頭,否則為 False

Example:

print(np.char.startswith(['Blair','Jane'],'r'))
print(np.char.startswith(['Blair','Jane'],'r',start=1,end=2))
print(np.char.startswith(['Blair','Jane'],'r',start=3,end=5))
s = np.array(['foo', 'bar'])
print(np.char.startswith(s, 'ar'))
print(np.char.startswith(s, 'a', start=1, end=2))
"""
[False False]
[False False]
[False False]
[False False]
[False  True]
"""

1.15 numpy.char.str_len

  char.str_len(a) 返回 len(a) 元素。

Example:

s = ['T','Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###','abc','ABC','1+2j#','',' ']
print(np.char.str_len(s))
"""
[1 5 4 6 4 4 3 3 3 3 5 0 1]
"""

 

相關文章