Lesson12——NumPy 字符串函数之 Part1:字符串操作函数
1 NumPy 字符串函数
以下函数用于对 dtype
为 numpy.string_
或 numpy.unicode_ 的数组执行向量化字符串操作。 它们基于 Python 内置库中的标准字符串函数。
这些函数在字符数组类(numpy.char
2 字符串操作函数
2.1 numpy.char.add()
print ('连接两个字符串:')
print (np.char.add(['hello'],[' Blair']))
print ('连接示例:')
print (np.char.add(['hello', 'hi'],[' Blair', ' Jane']))
"""
连接两个字符串:
['hello Blair']
连接示例:
['hello Blair' 'hi Jane']
"""
2.2 numpy.char.multiply()
numpy.char.multiply(a, i)
print (np.char.multiply('Blair ',3))
"""
Blair Blair Blair
"""
2.3 numpy.char.capitalize()
Example:
c = np.array(['a1b2','1b2a','b2a1','2a1b'],'S4')
print(c)
print(np.char.capitalize(c))
print(np.char.capitalize("blair"))
"""
[b'a1b2' b'1b2a' b'b2a1' b'2a1b']
[b'A1b2' b'1b2a' b'B2a1' b'2a1b']
Blair
"""
2.4 numpy.char.center()
Example:
print(np.char.center('Blair',width=8))
print(np.char.center('Blair',width=8,fillchar='0'))
"""
Blair
0Blair00
"""
相关方法:
numpy.char.zfill
Example:
print(np.char.zfill('Blair', width=10))
"""
00000Blair
"""
2.5 numpy.char.encode()
char.decode(a, encoding=None, errors=None)
Example:
x = np.char.encode('Blair') #默认编码是UTF-8
print (x)
x = np.char.encode('Blair', 'cp500')
print (x)
"""
b'Blair'
b'\xc2\x93\x81\x89\x99'
"""
2.6 numpy.char.encode()
char.decode(a, encoding=None, errors=None)
函数对编码的元素进行 str.decode()
解码。
Example:
x = np.char.encode('Blair') #默认编码是UTF-8
print (x)
print(np.char.decode(x))
x = np.char.encode('Blair', 'cp500')
print (x)
print(np.char.decode(x, 'cp500'))
"""
b'Blair'
Blair
b'\xc2\x93\x81\x89\x99'
Blair
"""
2.7 numpy.char.title()
char.title(a)
Example:
print (np.char.title('i like you'))
"""
I Like You
"""
2.8 numpy.char.lower()
numpy.char.lower(a)
函数对数组的每个元素转换为小写。它对每个元素调用 str.lower
。
Example:
c = np.array(['A1B C', '1BCA', 'BCA1'])
print(c)
print((c))
"""
['A1B C' '1BCA' 'BCA1']
['a1b c' '1bca' 'bca1']
"""
print(np.char.lower("I LIKE YOU"))
"""
i like you
"""
2.9 numpy.char.upper()
numpy.char.upper(a)
函数对数组的每个元素转换为大写。它对每个元素调用 str.upper
。
Example:
#操作数组
print (np.char.upper(['blair','google']))
# 操作字符串
print (np.char.upper('blair'))
"""
['BLAIR' 'GOOGLE']
BLAIR
"""
2.10 numpy.char.split()
Example:
# 分隔符默认为空格
print (np.char.split ('i like book?'))
# 分隔符为 .
print (np.char.split ('book1.book2.book3"', sep = '.'))
"""
['i', 'like', 'book?']
['book1', 'book2', 'book3"']
"""
2.11 numpy.char.splitlines()
char.splitlines(a, keepends=None)
Example:
# 换行符 \n
print (np.char.splitlines('i\nlike you?'))
print (np.char.splitlines('i\rlike you?'))
"""
['i', 'like you?']
['i', 'like you?']
"""
2.12 numpy.char.strip()
Example:
c = np.array(['aAaAaA', ' aA ', 'abBABba'])
print(c)
print(np.char.strip(c))
print(np.char.strip(c, 'a')) # 'a' unstripped from c[1] because whitespace leads
print(np.char.strip(c, 'A')) # 'A' unstripped from c[1] because (unprinted) ws trails
"""
['aAaAaA' ' aA ' 'abBABba']
['aAaAaA' 'aA' 'abBABba']
['AaAaA' ' aA ' 'bBABb']
['aAaAa' ' aA ' 'abBABba']
"""
Example:
# 移除字符串头尾的 a 字符
print (np.char.strip('ashok ablair','a'))
# 移除数组元素头尾的 a 字符
print (np.char.strip(['ablair','admin','java'],'a'))
"""
shok ablair
['blair' 'dmin' 'jav']
"""
相关方法:
numpy.char.lstrip
Example:
c = np.array(['aAaAaA', ' aA ', 'abBABba'])
print(c)
print(np.char.lstrip(c, 'a'))
"""
['aAaAaA' ' aA ' 'abBABba']
['AaAaA' ' aA ' 'bBABba']
"""
numpy.char.rstrip
Example:
c = np.array(['aAaAaA', 'abBABba'], dtype='S7')
print(c)
print(np.char.rstrip(c, b'a'))
print(np.char.rstrip(c, b'A'))
"""
[b'aAaAaA' b'abBABba']
[b'aAaAaA' b'abBABb']
[b'aAaAa' b'abBABba']
"""
2.13 numpy.char.join()
Example:
# 操作字符串
print (np.char.join(':','blair'))
# 指定多个分隔符操作数组元素
print (np.char.join([':','-'],['blair','google']))
"""
b:l:a:i:r
['b:l:a:i:r' 'g-o-o-g-l-e']
"""
2.14 numpy.char.replace()
numpy.char.replace(a, old, new, count=None)
函数使用新字符串替换字符串中的所有子字符串。
Example:
print (np.char.replace ('i like yoou', 'oo', 'u'))
"""
i like yuu
"""
2.15 numpy.char.ljust()
相关方法:
numpy.char.rjust
Example:
print(np.char.ljust('blair',width=10))
print(np.char.ljust('blair',width=10,fillchar='0'))
print(np.char.rjust('blair',width=10,fillchar='0'))
"""
blair
blair00000
00000blair
"""
char.swapcase(a)
以元素方式返回字符串的副本,其中大写字符转换为小写,反之亦然。
Example:
c=np.array(['a1B c','1b Ca','b Ca1','cA1b'],'S5');
print(c)
print(np.char.swapcase(c))
print(np.char.swapcase('I Love You'))
"""
[b'a1B c' b'1b Ca' b'b Ca1' b'cA1b']
[b'A1b C' b'1B cA' b'B cA1' b'Ca1B']
i lOVE yOU
"""
2.17 numpy.char.title
char.title(a)
Example:
c=np.array(['a1b c','1b ca','b ca1','ca1b'],'S5')
print(c)
print(np.char.title(c))
print(np.char.title("I LOVE YOU"))
"""
[b'a1b c' b'1b ca' b'b ca1' b'ca1b']
[b'A1B C' b'1B Ca' b'B Ca1' b'Ca1B']
I Love You
"""
因上求缘,果上努力~~~~ 作者:图神经网络,转载请注明原文链接:https://www.cnblogs.com/BlairGrowing/p/15894250.html