1 NumPy 字串函式
以下函式用於對 dtype
為 numpy.string_
或 numpy.unicode_ 的陣列執行向量化字串操作。 它們基於 Python 內建庫中的標準字串函式。
這些函式在字元陣列類(numpy.char
2 字串操作函式
2.1 numpy.char.add()
print ('連線兩個字串:')
print (np.char.add(['hello'],[' Blair']))
print ('連線示例:')
print (np.char.add(['hello', 'hi'],[' Blair', ' Jane']))
"""
連線兩個字串:
['hello Blair']
連線示例:
['hello Blair' 'hi Jane']
"""
2.2 numpy.char.multiply()
numpy.char.multiply(a, i)
print (np.char.multiply('Blair ',3))
"""
Blair Blair Blair
"""
2.3 numpy.char.capitalize()
Example:
c = np.array(['a1b2','1b2a','b2a1','2a1b'],'S4')
print(c)
print(np.char.capitalize(c))
print(np.char.capitalize("blair"))
"""
[b'a1b2' b'1b2a' b'b2a1' b'2a1b']
[b'A1b2' b'1b2a' b'B2a1' b'2a1b']
Blair
"""
2.4 numpy.char.center()
Example:
print(np.char.center('Blair',width=8))
print(np.char.center('Blair',width=8,fillchar='0'))
"""
Blair
0Blair00
"""
相關方法:
numpy.char.zfill
Example:
print(np.char.zfill('Blair', width=10))
"""
00000Blair
"""
2.5 numpy.char.encode()
char.decode(a, encoding=None, errors=None)
Example:
x = np.char.encode('Blair') #預設編碼是UTF-8
print (x)
x = np.char.encode('Blair', 'cp500')
print (x)
"""
b'Blair'
b'\xc2\x93\x81\x89\x99'
"""
2.6 numpy.char.encode()
char.decode(a, encoding=None, errors=None)
函式對編碼的元素進行 str.decode()
解碼。
Example:
x = np.char.encode('Blair') #預設編碼是UTF-8
print (x)
print(np.char.decode(x))
x = np.char.encode('Blair', 'cp500')
print (x)
print(np.char.decode(x, 'cp500'))
"""
b'Blair'
Blair
b'\xc2\x93\x81\x89\x99'
Blair
"""
2.7 numpy.char.title()
char.title(a)
Example:
print (np.char.title('i like you'))
"""
I Like You
"""
2.8 numpy.char.lower()
numpy.char.lower(a)
函式對陣列的每個元素轉換為小寫。它對每個元素呼叫 str.lower
。
Example:
c = np.array(['A1B C', '1BCA', 'BCA1'])
print(c)
print((c))
"""
['A1B C' '1BCA' 'BCA1']
['a1b c' '1bca' 'bca1']
"""
print(np.char.lower("I LIKE YOU"))
"""
i like you
"""
2.9 numpy.char.upper()
numpy.char.upper(a)
函式對陣列的每個元素轉換為大寫。它對每個元素呼叫 str.upper
。
Example:
#運算元組
print (np.char.upper(['blair','google']))
# 操作字串
print (np.char.upper('blair'))
"""
['BLAIR' 'GOOGLE']
BLAIR
"""
2.10 numpy.char.split()
Example:
# 分隔符預設為空格
print (np.char.split ('i like book?'))
# 分隔符為 .
print (np.char.split ('book1.book2.book3"', sep = '.'))
"""
['i', 'like', 'book?']
['book1', 'book2', 'book3"']
"""
2.11 numpy.char.splitlines()
char.splitlines(a, keepends=None)
Example:
# 換行符 \n
print (np.char.splitlines('i\nlike you?'))
print (np.char.splitlines('i\rlike you?'))
"""
['i', 'like you?']
['i', 'like you?']
"""
2.12 numpy.char.strip()
Example:
c = np.array(['aAaAaA', ' aA ', 'abBABba'])
print(c)
print(np.char.strip(c))
print(np.char.strip(c, 'a')) # 'a' unstripped from c[1] because whitespace leads
print(np.char.strip(c, 'A')) # 'A' unstripped from c[1] because (unprinted) ws trails
"""
['aAaAaA' ' aA ' 'abBABba']
['aAaAaA' 'aA' 'abBABba']
['AaAaA' ' aA ' 'bBABb']
['aAaAa' ' aA ' 'abBABba']
"""
Example:
# 移除字串頭尾的 a 字元
print (np.char.strip('ashok ablair','a'))
# 移除陣列元素頭尾的 a 字元
print (np.char.strip(['ablair','admin','java'],'a'))
"""
shok ablair
['blair' 'dmin' 'jav']
"""
相關方法:
numpy.char.lstrip
Example:
c = np.array(['aAaAaA', ' aA ', 'abBABba'])
print(c)
print(np.char.lstrip(c, 'a'))
"""
['aAaAaA' ' aA ' 'abBABba']
['AaAaA' ' aA ' 'bBABba']
"""
numpy.char.rstrip
Example:
c = np.array(['aAaAaA', 'abBABba'], dtype='S7')
print(c)
print(np.char.rstrip(c, b'a'))
print(np.char.rstrip(c, b'A'))
"""
[b'aAaAaA' b'abBABba']
[b'aAaAaA' b'abBABb']
[b'aAaAa' b'abBABba']
"""
2.13 numpy.char.join()
Example:
# 操作字串
print (np.char.join(':','blair'))
# 指定多個分隔符運算元組元素
print (np.char.join([':','-'],['blair','google']))
"""
b:l:a:i:r
['b:l:a:i:r' 'g-o-o-g-l-e']
"""
2.14 numpy.char.replace()
numpy.char.replace(a, old, new, count=None)
函式使用新字串替換字串中的所有子字串。
Example:
print (np.char.replace ('i like yoou', 'oo', 'u'))
"""
i like yuu
"""
2.15 numpy.char.ljust()
相關方法:
numpy.char.rjust
Example:
print(np.char.ljust('blair',width=10))
print(np.char.ljust('blair',width=10,fillchar='0'))
print(np.char.rjust('blair',width=10,fillchar='0'))
"""
blair
blair00000
00000blair
"""
char.swapcase(a)
以元素方式返回字串的副本,其中大寫字元轉換為小寫,反之亦然。
Example:
c=np.array(['a1B c','1b Ca','b Ca1','cA1b'],'S5');
print(c)
print(np.char.swapcase(c))
print(np.char.swapcase('I Love You'))
"""
[b'a1B c' b'1b Ca' b'b Ca1' b'cA1b']
[b'A1b C' b'1B cA' b'B cA1' b'Ca1B']
i lOVE yOU
"""
2.17 numpy.char.title
char.title(a)
Example:
c=np.array(['a1b c','1b ca','b ca1','ca1b'],'S5')
print(c)
print(np.char.title(c))
print(np.char.title("I LOVE YOU"))
"""
[b'a1b c' b'1b ca' b'b ca1' b'ca1b']
[b'A1B C' b'1B Ca' b'B Ca1' b'Ca1B']
I Love You
"""