numpy字符串处理

字符串拼接

numpy.char.add(x1, x2)
Return element-wise string concatenation for two arrays of str or unicode.
把字符串x1x2拼接在一起

示例:

>>>import numpy as np
>>>np.char.add("aaa","bbb")
array('aaabbb', dtype='<U6')
>>>np.char.add(["aaa"],["bbb"])
array(['aaabbb'], dtype='<U6')

numpy.char.join(sep, seq)
Return a string which is the concatenation of the strings in the sequence seq.
输入seq是一组字符串,用连接符sep把它们连接起来
示例:

>>>np.char.join('-',['a','b','c'])
array(['a', 'b', 'c'], dtype='<U1')

字符串转小/大写

转小写:numpy.char.lower(a)

转大写:numpy.char.upper(a)

示例:

>>> np.char.lower('aAbBcC')
array('aabbcc', dtype='<U6')
>>> np.char.upper('aAbBcC')
array('AABBCC', dtype='<U6')

字符串去掉最左/右边开头的元素

numpy.char.lstrip(a, chars=None)
For each element in a, return a copy with the leading characters removed.

参数说明:
chars {str, unicode}, optional
The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped.

输入a是一组字符串,对于a中的每个元素,如果最左边字母序列是chars,则被去掉

numpy.char.rstrip(a, chars=None)
For each element in a, return a copy with the trailing characters removed.

>>> c = np.array(['aAaAaA', '  aA  ', 'abBABba'])
>>> c
array(['aAaAaA', '  aA  ', 'abBABba'], dtype='<U7')
np.char.lstrip(c, 'a')
array(['AaAaA', '  aA  ', 'bBABba'], dtype='<U7')

字符串分割

numpy.char.split(a, sep=None, maxsplit=None)
For each element in a, return a list of the words in the string, using sep as the delimiter string.

参数说明:
sep str or unicode, optional
If sep is not specified or None, any whitespace string is a separator.
maxsplit int, optional
If maxsplit is given, at most maxsplit splits are done.
输入a是一组字符串,对于a中的每个元素,对其进行检索,如果遇到sep,就在这个位置给字符串打个隔断

示例

>>>np.char.split('a b c',' ')
array(list(['a', 'b', 'c']), dtype=object)

字符串结尾判断

char.endswith(a, suffix, start=0, end=None)
Returns a boolean array which is True where the string element in a ends with suffix, otherwise False.
参数说明:
a array_like of str or unicode
suffix str
start, end int, optional
With optional start, test beginning at that position. With optional end, stop comparing at that position.

Returns
out ndarray
Outputs an array of bools.

输入a是一个字符串序列,依次判断a中的每个字符串是否以suffix结尾,如果指定了startend,则每个字符串截取[start,end)的片段进行判断.返回一个和a等长的布尔array.

示例

>>>s = np.array(['foo', 'bar'])
>>>np.char.endswith(s, 'ar')
array([False,  True])
>>>np.char.endswith(s, 'a', start=1, end=2)
array([False,  True])

字符串查找

char.find(a, sub, start=0, end=None)

For each element, return the lowest index in the string where substring sub is found.

参数说明:
a array_like of str or unicode
sub str
start, end int, optional
With optional start, test beginning at that position. With optional end, stop comparing at that position.

Returns
out ndarray
Outputs an array of bools.

输入a 是一个字符串数组, 对于a中每个字符串,寻找sub第一次出现的index, 如果没找到,返回-1,startend的功能同上。返回的是一个和a等长的数组。

参考博客

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值