以下函数用于对dtype
为numpy.string_
或numpy.unicode_
的数组执行向量化字符串操作。 它们基于 Python 内置库中的标准字符串函数。
序号 | 函数及描述 |
1. |
|
2. |
|
3. |
|
4. |
|
5. |
|
6. |
|
7. |
|
8. |
|
9. |
|
10. |
|
11. |
|
12. |
|
13. |
|
14. |
|
这些函数在字符数组类(numpy.char
)中定义。 较旧的 Numarray 包包含chararray
类。 numpy.char
类中的上述函数在执行向量化字符串操作时非常有用。
numpy.char.add()
函数执行按元素的字符串连接。
import numpy as np
print '连接两个字符串:' print np.char.add(['hello'],[' xyz']) print '\n' print '连接示例:' print np.char.add(['hello', 'hi'],[' abc', ' xyz'])
import numpy as np
print '连接两个字符串:' print np.char.add(['hello'],[' xyz']) print '\n' print '连接示例:' print np.char.add(['hello', 'hi'],[' abc', ' xyz'])
Python
输出如下:
连接两个字符串:
['hello xyz']
连接示例:
['hello abc' 'hi xyz']
numpy.char.multiply()
这个函数执行多重连接。
import numpy as np
print np.char.multiply('Hello ',3)
import numpy as np
print np.char.multiply('Hello ',3)
Python
输出如下:
Hello Hello Hello
Hello Hello Hello
Python
numpy.char.center()
此函数返回所需宽度的数组,以便输入字符串位于中心,并使用fillchar
在左侧和右侧进行填充。
import numpy as np
# np.char.center(arr, width,fillchar)
print np.char.center('hello', 20,fillchar = '*')
import numpy as np
# np.char.center(arr, width,fillchar)
print np.char.center('hello', 20,fillchar = '*')
Python
输出如下:
*******hello********
*******hello********
Python
numpy.char.capitalize()
函数返回字符串的副本,其中第一个字母大写
import numpy as np
print np.char.capitalize('hello world')
import numpy as np
print np.char.capitalize('hello world')
Python
输出如下:
Hello world
numpy.char.title()
返回输入字符串的按元素标题转换版本,其中每个单词的首字母都大写。
import numpy as np
print np.char.title('hello how are you?')
import numpy as np
print np.char.title('hello how are you?')
Python
输出如下:
Hello How Are You?
numpy.char.lower()
函数返回一个数组,其元素转换为小写。它对每个元素调用str.lower
。
import numpy as np
print np.char.lower(['HELLO','WORLD']) print np.char.lower('HELLO')
import numpy as np
print np.char.lower(['HELLO','WORLD']) print np.char.lower('HELLO')
Python
输出如下:
['hello' 'world'] hello
['hello' 'world'] hello
Python
numpy.char.upper()
函数返回一个数组,其元素转换为大写。它对每个元素调用str.upper
。
import numpy as np
print np.char.upper('hello') print np.char.upper(['hello','world'])
import numpy as np
print np.char.upper('hello') print np.char.upper(['hello','world'])
Python
输出如下:
HELLO
['HELLO' 'WORLD']
HELLO
['HELLO' 'WORLD']
Python
numpy.char.split()
此函数返回输入字符串中的单词列表。 默认情况下,空格用作分隔符。 否则,指定的分隔符字符用于分割字符串。
import numpy as np
print np.char.split ('hello how are you?') print np.char.split ('YiibaiPoint,Hyderabad,Telangana', sep = ',')
import numpy as np
print np.char.split ('hello how are you?') print np.char.split ('YiibaiPoint,Hyderabad,Telangana', sep = ',')
Python
输出如下:
['hello', 'how', 'are', 'you?'] ['YiibaiPoint', 'Hyderabad', 'Telangana']
['hello', 'how', 'are', 'you?'] ['YiibaiPoint', 'Hyderabad', 'Telangana']
Python
numpy.char.splitlines()
函数返回数组中元素的单词列表,以换行符分割。
import numpy as np
print np.char.splitlines('hello\nhow are you?') print np.char.splitlines('hello\rhow are you?')
import numpy as np
print np.char.splitlines('hello\nhow are you?') print np.char.splitlines('hello\rhow are you?')
Python
输出如下:
['hello', 'how are you?'] ['hello', 'how are you?']
['hello', 'how are you?'] ['hello', 'how are you?']
Python
'\n'
,'\r'
,'\r\n'
都会用作换行符。
numpy.char.strip()
函数返回数组的副本,其中元素移除了开头或结尾处的特定字符。
import numpy as np
print np.char.strip('ashok arora','a') print np.char.strip(['arora','admin','java'],'a')
import numpy as np
print np.char.strip('ashok arora','a') print np.char.strip(['arora','admin','java'],'a')
Python
输出如下:
shok aror
['ror' 'dmin' 'jav']
numpy.char.join()
这个函数返回一个字符串,其中单个字符由特定的分隔符连接。
import numpy as np
print np.char.join(':','dmy') print np.char.join([':','-'],['dmy','ymd'])
import numpy as np
print np.char.join(':','dmy') print np.char.join([':','-'],['dmy','ymd'])
Python
输出如下:
d:m:y
['d:m:y' 'y-m-d']
d:m:y
['d:m:y' 'y-m-d']
Python
numpy.char.replace()
这个函数返回字符串副本,其中所有字符序列的出现位置都被另一个给定的字符序列取代。
import numpy as np
print np.char.replace ('He is a good boy', 'is', 'was')
import numpy as np
print np.char.replace ('He is a good boy', 'is', 'was')
Python
输出如下:
He was a good boy
numpy.char.decode()
这个函数在给定的字符串中使用特定编码调用str.decode()
。
import numpy as np
a = np.char.encode('hello', 'cp500') print a print np.char.decode(a,'cp500')
import numpy as np
a = np.char.encode('hello', 'cp500') print a print np.char.decode(a,'cp500')
Python
输出如下:
\x88\x85\x93\x93\x96
hello
numpy.char.encode()
此函数对数组中的每个元素调用str.encode
函数。 默认编码是utf_8
,可以使用标准 Python 库中的编解码器。
import numpy as np
a = np.char.encode('hello', 'cp500') print a
import numpy as np
a = np.char.encode('hello', 'cp500') print a
Python
输出如下:
\x88\x85\x93\x93\x96