numpy学习专题
十五、numpy字符串函数
1.字符串函数
是用于对dtype为numpy.string或numpy.unicode的数组执行向量化字符串操作
基于python内置库中的标准字符串函数在字符串组类(numpy.char)中定义
1.add()
对两个数组的元素进行字符串拼接
2.multiply()
返回按元素多重连接后的字符串
3.center()
将字符串居中,并使用指定字符在左侧和右侧进行填充
4.capitalize()
将字符串的第一个字母大写
5.title()
将字符串的每个单词的第一个字母大写
6.lower()
数组元素转换为小写
7.upper()
数组元素转换为大写
8.split()
指定分隔符对字符串进行分割,并返回数组列表
9.splitlines()
返回元素中的行列表,以换行符分割
10.strip()
移除元素开头或者结尾处的特定字符
11.replace()
使用新字符替换字符串中的所有字符串
12.encode()
编码,数组元素依次调用str.encode
13.decode()
解码,数组元素依次调用str.decode
import numpy as np
print(np.char.add(["sunck"],["good"]))
print(np.char.add(["sunck","kaige"],["good","nice"]))
['sunckgood']
['sunckgood' 'kaigenice']
import numpy as np
print(np.char.multiply(["ZJM"],3))
['ZJMZJMZJM']
import numpy as np
print(np.char.center(["nice","good"],10,fillchar="&"))
['&&&nice&&&' '&&&good&&&']
import numpy as np
print(np.char.capitalize(["zJM is a handsome boy","xZQ is a handsome singer"]))
['Zjm is a handsome boy' 'Xzq is a handsome singer']
import numpy as np
print(np.char.title(["zjm is a handsome boy","xzq is a handsome singer"]))
['Zjm Is A Handsome Boy' 'Xzq Is A Handsome Singer']
import numpy as np
print(np.char.lower([" ZJM IS A HANDSOME BOY","XZQ IS A HANDSOME SINGER"]))
[' zjm is a handsome boy' 'xzq is a handsome singer']
import numpy as np
print(np.char.upper([" zjm is a handsome boy","xzq is a handsome singer"]))
[' ZJM IS A HANDSOME BOY' 'XZQ IS A HANDSOME SINGER']
import numpy as np
print(np.char.split(["ZJM IS A HANDSOME BOY","xzq is a handsome singer"],sep=" "))
[list(['ZJM', 'IS', 'A', 'HANDSOME', 'BOY'])
list(['xzq', 'is', 'a', 'handsome', 'singer'])]
import numpy as np
print(np.char.splitlines(["ZJM\n IS A HANDSOME BOY","xzq\n is a handsome singer"]))
[list(['ZJM', ' IS A HANDSOME BOY'])
list(['xzq', ' is a handsome singer'])]
import numpy as np
print(np.char.strip(["***ZJM***","***XZQ**"],"*"))
['ZJM' 'XZQ']
import numpy as np
print(np.char.replace(["ZJM IS A HANDSOME BOY","XZQ IS A HANDSOME SINGER"],"HANDSOME","HARDWORKING"))
['ZJM IS A HARDWORKING BOY' 'XZQ IS A HARDWORKING SINGER']
import numpy as np
print(np.char.encode(["nice","good"],"utf-8"))
[b'nice' b'good']
import numpy as np
arr = np.char.encode(["nice","good"],"utf-8")
print(np.char.decode(arr,"utf-8"))
['nice' 'good']
一定要支持作者哦