NumPy学习笔记13.字符串函数

NumPy字符串函数

表格中的函数在字符数组类(numpy.char)中定义:

函数 描述
add() 对两个数组的逐个字符串元素进行连接
multiply() 返回按元素多重连接后的字符串
center() 居中字符串
capitalize() 将字符串第一个字母转换为大写
title() 将字符串的每个单词的第一个字母转换为大写
lower() 数组元素转换为小写
upper() 数组元素转换为大写
split() 指定分隔符对字符串进行分割,并返回数组列表
splitlines() 返回元素中的行列表,以换行符分割
strip() 指定分隔符对字符串进行分割,并返回数组列表
splitlines() 返回元素中的行列表,以换行符分割\
strip() 移除元素开头或结尾处的特定字符
join() 通过指定分隔符来连接数组中的元素
replace() 使用新字符串换字符串中的所有字符串
decode() 数组元素依次调用str.decode
encode() 数组元素依次调用str.encode
numpy.char.add()
import numpy as np

print('连接两个字符串:')
print(np.char.add(['hello'],['xyz']))
print('\n')

print('连接示例:')
print(np.char.add(['hello','hi'],[' python',' numpy']))
连接两个字符串:
['helloxyz']


连接示例:
['hello python' 'hi numpy']
numpy.char.multiply()
import numpy as np
print(np.char.multiply('Python ', 3))
Python Python Python 
numpy.char.center()
import numpy as np

# np.char.center(str,width,fillchar):
# str:字符串,width:长度,fillchar:填充字符
print(np.char.center('NumPy',20,fillchar = '*'))
*******NumPy********
numpy.char.capitalize()
import numpy as np

print(np.char.capitalize('python'))
Python
numpy.char.title()
import numpy as np

print(np.char.title('i like python'))
I Like Python
numpy.char.lower()
import numpy as np

# 操作数组
print(np.char.lower(['PYTHON','NUMPY']))

# 操作字符串
print(np.char.lower('PYTHON'))
['python' 'numpy']
python
numpy.char.upper()
import numpy as np

# 操作数组
print(np.char.upper(['python','numpy']))

# 操作字符串
print(np.char.upper('numpy'))
['PYTHON' 'NUMPY']
PYTHON
numpy.char.split()
import numpy as np

# 分隔符默认为空格
print(np.char.split('i like numpy'))
# 分隔符为 .
print(np.char.split('www.numpy.com',sep = '.'))
['i', 'like', 'numpy']
['www', 'numpy', 'com']
numpy.char.splitlines()
import numpy as np

print(np.char.splitlines('i\nlike numpy'))
print(np.char.splitlines('i\rlike numpy'))
['i', 'like numpy']
['i', 'like numpy']    
numpy.char.strip()
import numpy as np

# 移除字符串头尾的a字符
print(np.char.strip('apython anumpy','a'))

# 移除数组元素头尾的a字符
print(np.char.strip(['apythona','anumpya'],'a'))
python anumpy
['python' 'numpy']
numpy.char.join()
import numpy as np

# 操作字符串
print(np.char.join(':','python'))

# 指定多个分隔符操作数组元素
print(np.char.join([':','-'],['python','numpy']))
p:y:t:h:o:n
['p:y:t:h:o:n' 'n-u-m-p-y']
numpy.char.replace()
import numpy as np

print(np.char.replace('i like python','thon','charm'))
i like pycharm
numpy.char.encode()
import numpy as np

a = np.char.encode('python','cp500')
print(a)
b'\x97\xa8\xa3\x88\x96\x95'
numpy.char.decode()
import numpy as np

a = np.char.encode('python','cp500')
print(a)
print(np.char.decode(a,'cp500'))
b'\x97\xa8\xa3\x88\x96\x95'
python

学习参考:

  • 菜鸟教程:https://www.runoob.com/numpy/numpy-tutorial.html

你可能感兴趣的:(#,NumPy,NumPy,numpy.char,字符串函数)