分享

NumPy字符串处理

 LibraryPKU 2019-07-19

《NumPy字符串处理》

简介

NumPy中也有专门处理字符串的方法,这些方法包含在numpy.char中。

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

接下来一一作介绍。

add()函数

该函数和字符串操作的+或者join()函数一样,能够连接两个字符串。

代码演示:

1
2
3
4
5
>>>import numpy as np
>>>print(np.char.add('hello ', 'world'))
hello world
>>>print(np.char.add(['hello ', 'welcome '], ['finthon', 'to my world']))
['hello finthon' 'welcome to my world']

multiply()函数

该函数可以将指定的字符串重复n次。

代码演示:

1
2
3
>>>import numpy as np
>>>print(np.char.multiply('finthon ',3))
finthon finthon finthon

center()函数

该函数可以将字符串居中对齐,并能指定两端填充的字符类型。

代码演示:

1
2
3
>>>import numpy as np
>>>print(np.char.center('finthon', 30, fillchar='*'))
***********finthon************

在这里我们将字符串'finthon'居中,两端通过fillchar关键字指定填充字符,加起来的总长度为30

capitalize()函数

该函数会将字符串的第一个字母大写。

代码演示:

1
2
3
>>>import numpy as np
>>>print(np.char.capitalize('finthon'))
Finthon

title()函数

该函数会将每个单词的首字母大写,变成标题的形式。

代码演示:

1
2
3
>>>import numpy as np
>>>print(np.char.title('welcome to my world'))
Welcome To My World

lower()函数

该函数可以将所有字母变成小写。

代码演示:

1
2
3
>>>import numpy as np
>>>print(np.char.lower('I liKe FINTHON.com'))
i like

upper()函数

该函数可以将所有字母变成大写。

代码演示:

1
2
3
>>>import numpy as np
>>>print(np.char.upper('I liKe FINTHON.com'))
I LIKE FINTHON.COM

split()函数

该函数可以将字符串分割,默认以空格为分隔符,当然也可以指定特定的分隔符。

代码演示:

1
2
3
4
5
6
>>>import numpy as np
>>>print(np.char.split('welcome to my website '))
['welcome', 'to', 'my', 'website', '']
>>>print(np.char.split('i$like$finthon', sep='$'))
['i', 'like', 'finthon']

splitlines()函数

该函数可以用来分割换行符(\n, \r, \r\n)。

代码演示:

1
2
3
4
5
6
7
8
9
>>>import numpy as np
>>>print(np.char.splitlines('hello, world\nmy name is finthon!'))
['hello, world', 'my name is finthon!']
>>>print(np.char.splitlines('this is a test\r for numpy'))
['this is a test', ' for numpy']
>>>print(np.char.splitlines('i like\r\nfinthon'))
['i like', 'finthon']

strip()函数

该函数能够移除字符串首尾的字符,默认为空格。

代码演示:

1
2
3
4
5
6
7
8
9
>>>import numpy as np
>>>print(np.char.strip('  finthon  '))
finthon
>>>print(np.char.strip('finally finthon', 'f'))
inally finthon
>>>print(np.char.strip(['finally', 'fif', 'finthon'], 'f'))
['inally' 'i' 'inthon']

可以看出不仅可以传入一个字符串,还可以传入一个列表。传入列表时,就是对列表中的每个元素进行操作。在这里我们制定去除首尾的'f'

join()函数

该函数可以通过指定的分隔符连接字符串中的每个元素。

代码演示:

1
2
3
4
5
6
>>>import numpy as np
>>>print(np.char.join('.', 'finthon'))
f.i.n.t.h.o.n
>>>print(np.char.join(['.', '*'], ['hello', 'world']))
['h.e.l.l.o' 'w*o*r*l*d']

在这里可以传入字符串或一个列表。

replace()函数

该函数能用指定的字符替换原字符串指定的元素。

代码演示:

1
2
3
>>>import numpy as np
>>>print(np.char.replace('I like you', 'you', 'finthon'))
I like finthon

在这里我们传入一个字符串'I like you',然后选择使用'finthon'替换'you‘。

encode()函数

该函数可以对字符串进行编码,可以指定编码的方式,默认为utf-8,如果对编码不熟悉的,可以参照Python中编码解码过程

代码演示:

1
2
3
4
5
6
>>>import numpy as np
>>>print(np.char.encode('finthon'))
b'finthon'
>>>print(np.char.encode('finthon', 'utf-16'))
b'\xff\xfef\x00i\x00n\x00t\x00h\x00o\x00n'

decode()函数

该函数可以将机器码按照指定的方式解码。

代码演示:

1
2
3
4
>>>import numpy as np
>>>a = np.char.encode('hello', 'cp500')
>>>print(np.char.decode(a, 'cp500'))
hello

总结

本文介绍了NumPy中字符串处理的相关函数。字符串在NumPy中的使用也比较少,但是相关的方法还是和string比较类似,学习起来比较简单。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多