字符串函数
- # 在 numpy 中使用 numpy.char 调用函数
- import numpy as np
- a = np.char.add(['ss','zz'],['yy','xx']) # add(), 将两个数组对应位置的元素逐个连接
- print(a) # 结果为 ['ssyy','zzxx']
- b = np.char.multiply(['sy','sz'],3) # multiply(), 将元素重复指定次数连接
- print(b) # 结果为 ['sysysy','szszsz']
- c = np.char.center('call',10,fillchar='#') # center(), 将元素在指定宽度内居中, fillchar 指定用什么填充
- print(c)
- d = np.char.capitalize(['cool boy','base']) # capitalize(), 将每个元素首字母大写
- print(d)
- e = np.char.title('hello world') # title(), 将每个单词的首字母大写
- print(e)
- f = np.char.lower('HELLO') # lower(), 将每个大写的字母转换成小写
- print(f)
- g = np.char.upper('hello') # upper(), 将每个小写的字母转换成大写
- print(g)
- h = np.char.split('www.baidu.com',sep='.') # split(), 按照指定的符号分割, 默认是按空格分割, 返回值放在一个数组内
- print(h) # splitlines(), 按照换行符分割
- i = np.char.strip('hello world',chars='h') # strip(), 移除首尾指定的字符, 默认移除空格
- print(i)
- j = np.char.join(['-','*'],['hello','world']) # join(), 用指定的字符插入到元素的每个字符之间
- print(j) # 结果为 ['h-e-l-l-o' 'w*o*r*l*d']
- k = np.char.replace('hello','ello','i') # replace(), 新的字符替换原字符
- print(k)
- p = np.char.encode('hello','gbk') # encode(), 设置编码格式
- p1 = np.char.decode(p,'gbk') # decode(), 解码
- print(p)
- print(p1)
来源: http://www.bubuko.com/infodetail-3340597.html