函數 1.字元串函數 #(1)add() 對兩個數組的元素進行字元串連接 import numpy as np print(np.char.add(["xiaodu"],["good"])) print(np.char.add(["xiaodu","dudu"],["good","nice"])) ...
函數
1.字元串函數
#(1)add() 對兩個數組的元素進行字元串連接 import numpy as np print(np.char.add(["xiaodu"],["good"])) print(np.char.add(["xiaodu","dudu"],["good","nice"]))
#output:['xiaodugood'] ['xiaodugood' 'dudunice']
#(2)multiply() 返回按元素多重連接後的字元串 import numpy as np print(np.char.multiply("good",3))
#output:goodgoodgood
#(3)center() 將字元串居中,並使用指定字元在左側和右側進行填充 import numpy as np print(np.char.center(["nice","good"],12,fillchar="*"))
#output:['****nice****' '****good****']
#(4)capitalize() 將字元串的第一個字母轉換為大寫 import numpy as np print(np.char.capitalize(["what a nice day today","i have a good time"]))
#output:['What a nice day today' 'I have a good time']
#(5)title() 將字元串的每個單詞的第一個字母轉換為大寫 import numpy as np print(np.char.title(["what a nice day today","i have a good time"]))
#output: ['What A Nice Day Today' 'I Have A Good Time']
#(6)lower() 數組元素轉換為小寫 import numpy as np print(np.char.lower(["what a nice Day today","i HAVE a good time"]))
#Output:['what a nice day today' 'i have a good time']
#7)upper() 數組元素轉換為大寫 import numpy as np print(np.char.upper(["what a nice Day today","i HAVE a good time"]))
#Output: ['WHAT A NICE DAY TODAY' 'I HAVE A GOOD TIME']
#(8)split() 指定分隔符對字元串進行分割,並返回數組列表 import numpy as np print(np.char.split(["what a nice day today","i have a good time"],sep=" "))
#Output: [list(['what', 'a', 'nice', 'day', 'today']) list(['i', 'have', 'a', 'good', 'time'])]
#(9)splitlines() 返回元素中的行列表,以換行符分割 import numpy as np print(np.char.splitlines(["what a nice\nday today","i have a good\ntime"]))
#output: [list(['what a nice', 'day today']) list(['i have a good', 'time'])]
#(10)strip() 移除元素開頭或者結尾處的特定字元 import numpy as np print(np.char.strip(["**nice***","*day"],"*"))
#output: ['nice' 'day']
#(11)join() 通過指定分隔符來連接數組中的元素 import numpy as np print(np.char.join("-",["nice","day"])) print(np.char.join(["-",":"],["nice","day"]))
#output: ['n-i-c-e' 'd-a-y'] ['n-i-c-e' 'd:a:y']
#(12)replace() 使用新字元串替換字元串中的所有子字元串 import numpy as np print(np.char.replace(["what a good day today","i have a good time"],"good","nice"))
#output: ['what a nice day today' 'i have a nice time']
#(13)encode() 編碼,數組元素依次調用str.encode import numpy as np print(np.char.encode(["nice","good"],"utf-8"))
#(14)decode() 解碼,數組元素依次調用str.decode import numpy as np a = np.char.encode(["nice","good"],"utf-8") print(np.char.decode(a,"utf-8"))
#output: ['nice' 'good']