numpy字符串函数

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
#1.add() 字符串拼接函数
import numpy as np
print(np.char.add(["sunck"],["good"]))
print(np.char.add(["sunck","kaige"],["good","nice"]))
['sunckgood']
['sunckgood' 'kaigenice']
#1.multiply() 字符串多重连接函数
import numpy as np
print(np.char.multiply(["ZJM"],3))
['ZJMZJMZJM']
#3.center() 字符串居中填充函数
import numpy as np
print(np.char.center(["nice","good"],10,fillchar="&"))
['&&&nice&&&' '&&&good&&&']
#4.capitalize() 字符串首字母大写函数
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']
#5.title() 单词首字母大写函数
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']
#6.lower() 字符串小写函数
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']
#7.upper() 字符串大写函数
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']
#8.split() 字符串分割函数
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'])]
#9.splitlines() 字符串分割函数
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'])]
#10.strip() 字符串移除函数
import numpy as np
print(np.char.strip(["***ZJM***","***XZQ**"],"*"))
['ZJM' 'XZQ']
#11.replace() 字符串替换函数
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']
#11.encode() 字符串编码函数
import numpy as np
print(np.char.encode(["nice","good"],"utf-8"))
[b'nice' b'good']
#12.decode() 字符串解码函数
import numpy as np
arr = np.char.encode(["nice","good"],"utf-8")
print(np.char.decode(arr,"utf-8"))
['nice' 'good']

一定要支持作者哦

你可能感兴趣的:(numpy,python,开发语言)