NumPy字符串函数

NumPy字符串函数

NumPy字符串函数

  NumPy以下函数用于对数据类型 numpy.string_ 或 numpy.unicode_ 的数组执行向量化字符串操作。它们基于 Python 内置库中的标准字符串函数。

  推荐:如何将NumPy数组复制到另一个数组

1、add()

add

  返回两个 str 或 Unicode 数组的按元素字符串连接

import numpy as np 
print('Concatenate two strings:')
print(np.char.add(['hello'],[' xyz']))
print('\n')

print('Concatenation example:')
print(np.char.add(['hello', 'hi'],[' abc', ' xyz']))

输出:
Concatenate two strings:
['hello xyz']


Concatenation example:
['hello abc' 'hi xyz']

2、multiply()

  返回具有多个元素连接的字符串

import numpy as np 
print(np.char.multiply('Hello ',3))

输出:
Hello Hello Hello 

3、center()

  返回给定字符串的副本,其中元素以指定长度的字符串为中心

import numpy as np 
# np.char.center(arr, width,fillchar) 
print(np.char.center('hello', 20,fillchar = '*'))

输出:
*******hello********

  推荐:创建一个全为零的Numpy数组

4、capitalize()

  返回仅第一个字符大写的字符串的副本

import numpy as np 
print(np.char.capitalize('hello world'))

输出:
Hello world

5、title()

title

  返回字符串或 unicode 的按元素标题大小写版本

import numpy as np 
print(np.char.title('hello how are you?'))

输出:
Hello How Are You?

6、lower()

  返回一个数组,其中元素转换为小写

import numpy as np 
print(np.char.lower(['HELLO','WORLD'])) 
print(np.char.lower('HELLO'))

输出:
['hello' 'world']
hello

  推荐:NumPy二元运算符

7、upper()

  返回一个数组,其中元素转换为大写

import numpy as np 
print(np.char.upper('hello'))
print(np.char.upper(['hello','world']))

输出:
HELLO
['HELLO' 'WORLD']

8、split()

  使用分隔符分隔符返回字符串中单词的列表

import numpy as np 
print(np.char.split ('hello how are you?'))
print(np.char.split ('TutorialsPoint,Hyderabad,Telangana', sep = ','))

输出:
['hello', 'how', 'are', 'you?']
['TutorialsPoint', 'Hyderabad', 'Telangana']

9、splitlines()

  返回元素中的行列表,在行边界处断开

import numpy as np 
print(np.char.splitlines('hello\nhow are you?'))
print(np.char.splitlines('hello\rhow are you?'))

输出:
['hello', 'how are you?']
['hello', 'how are you?']

  推荐:NumPy数据类型

10、strip()

strip

  返回删除了前导和尾随字符的副本

import numpy as np 
print(np.char.strip('ashok arora','a'))
print(np.char.strip(['arora','admin','java'],'a'))

输出:
shok aror
['ror' 'dmin' 'jav']

11、join()

  返回一个字符串,它是序列中字符串的串联

import numpy as np 
print(np.char.join(':','dmy'))
print(np.char.join([':','-'],['dmy','ymd']))

输出:
d:m:y
['d:m:y' 'y-m-d']

12、replace()

  返回字符串的副本,其中所有出现的子字符串都被新字符串替换

import numpy as np 
print(np.char.replace ('He is a good boy', 'is', 'was'))

输出:
He was a good boy

13、decode()

  按元素调用 str.decode

import numpy as np 

a = np.char.encode('hello', 'cp500') 
print(a)
print(np.char.decode(a,'cp500'))

输出:
b'\x88\x85\x93\x93\x96'
hello

14、encode()

  按元素调用 str.encode

import numpy as np 
a = np.char.encode('hello', 'cp500') 
print(a)

输出:
b'\x88\x85\x93\x93\x96'

  推荐:Python中使用Numpy创建含零的Numpy数组

  推荐:Numpy教程


晓得博客,版权所有丨如未注明,均为原创
晓得博客 » NumPy字符串函数

转载请保留链接:https://www.pythonthree.com/numpy-string-functions/

滚动至顶部