字符串功能#

numpy.strings 模块提供了一组通用函数,用于操作 numpy.str_numpy.bytes_ 类型的数组.例如,

>>> np.strings.add(["num", "doc"], ["py", "umentation"])
array(['numpy', 'documentation'], dtype='<U13')

这些通用函数也用于 numpy.char 中,它提供了 numpy.char.chararray 数组子类,以便这些例程也能获得性能优势.

备注

在 NumPy 2.0 之前,所有字符串功能都在 numpy.char 中,它只对固定宽度的字符串进行操作.该模块将不会得到更新,并且在将来某个时候将被弃用.

字符串操作#

add (x1, x2, /[, out, where, casting, order, ...])

按元素方式相加参数.

center (a, width[, fillchar])

返回 a 的副本,其元素在长度为 width 的字符串中居中对齐.

capitalize (a)

返回 a 的副本,其中每个元素的第一个字符大写.

decode (a[, encoding, errors])

按元素调用 bytes.decode .

encode (a[, encoding, errors])

逐元素调用 str.encode .

expandtabs (a[, tabsize])

返回每个字符串元素的副本,其中所有制表符都替换为一个或多个空格.

ljust (a, width[, fillchar])

返回一个数组,该数组中的元素在长度为 width 的字符串中左对齐.

lower (a)

返回一个将元素转换为小写的数组.

lstrip (a[, chars])

对于 a 中的每个元素,返回一个删除了前导字符的副本.

mod (a, values)

返回 (a % i),即 Python 2.6 之前的字符串格式化(插值),对于 str 或 unicode 的 array_likes 对,逐个元素执行.

multiply (a, i)

返回 (a * i),即字符串多重连接,逐个元素执行.

partition (a, sep)

围绕 sep 分区 a 中的每个元素.

replace (a, old, new[, count])

对于 a 中的每个元素,返回字符串的副本,并将子字符串 old 的出现替换为 new .

rjust (a, width[, fillchar])

返回一个数组,该数组中的元素在长度为 width 的字符串中右对齐.

rpartition (a, sep)

围绕最右边的分隔符分区(拆分)每个元素.

rstrip (a[, chars])

对于 a 中的每个元素,返回一个删除尾随字符的副本.

slice (a[, start, stop, step])

通过 start , stop , step 指定的切片切割 a 中的字符串.

strip (a[, chars])

对于 a 中的每个元素,返回一个删除前导和尾随字符的副本.

swapcase (a)

按元素返回字符串的副本,其中大写字符转换为小写,反之亦然.

title (a)

按元素返回字符串或 unicode 的首字母大写版本.

translate (a, table[, deletechars])

对于 a 中的每个元素,返回一个字符串的副本,其中删除了可选参数 deletechars 中出现的所有字符,并且剩余的字符已通过给定的翻译表进行了映射.

upper (a)

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

zfill (a, width)

返回用零填充的数值字符串.

比较#

numpy.strings 模块还导出了比较通用函数,它们现在也可以对字符串数组进行操作.

equal (x1, x2, /[, out, where, casting, ...])

按元素返回 (x1 == x2).

not_equal (x1, x2, /[, out, where, casting, ...])

逐个元素返回 (x1 != x2).

greater_equal (x1, x2, /[, out, where, ...])

逐元素返回 (x1 >= x2) 的真值.

less_equal (x1, x2, /[, out, where, casting, ...])

按元素方式返回 (x1 <= x2) 的真值.

greater (x1, x2, /[, out, where, casting, ...])

逐元素返回 (x1 > x2) 的真值.

less (x1, x2, /[, out, where, casting, ...])

按元素方式返回 (x1 < x2) 的真值.

字符串信息#

count (a, sub[, start, end])

返回一个数组,其中包含子字符串 sub 在范围 [ start , end ) 中非重叠出现的次数.

endswith (a, suffix[, start, end])

返回一个布尔数组,如果 a 中的字符串元素以 suffix 结尾,则为 True ,否则为 False .

find (a, sub[, start, end])

对于每个元素,返回字符串中找到子字符串 sub 的最低索引,使得 sub 包含在范围 [ start , end ) 中.

index (a, sub[, start, end])

find 类似,但当未找到子字符串时引发 ValueError .

isalnum (x, /[, out, where, casting, order, ...])

如果字符串中的所有字符都是字母数字,并且至少有一个字符,则为每个元素返回 true,否则返回 false.

isalpha (x, /[, out, where, casting, order, ...])

如果解释为字符串的数据中的所有字符都是字母并且至少有一个字符,则为每个元素返回 true,否则返回 false.

isdecimal (x, /[, out, where, casting, ...])

对于每个元素,如果元素中只有十进制字符,则返回 True.

isdigit (x, /[, out, where, casting, order, ...])

如果字符串中的所有字符都是数字,并且至少有一个字符,则为每个元素返回 true,否则返回 false.

islower (x, /[, out, where, casting, order, ...])

如果字符串中的所有区分大小写的字符都是小写,并且至少有一个区分大小写的字符,则为每个元素返回 true,否则返回 false.

isnumeric (x, /[, out, where, casting, ...])

对于每个元素,如果元素中只有数字字符,则返回 True.

isspace (x, /[, out, where, casting, order, ...])

如果字符串中只有空白字符,并且至少有一个字符,则为每个元素返回 true,否则返回 false.

istitle (x, /[, out, where, casting, order, ...])

如果元素是标题字符串,并且至少有一个字符,则为每个元素返回 true,否则返回 false.

isupper (x, /[, out, where, casting, order, ...])

如果字符串中所有区分大小写的字符均为大写且至少有一个字符,则为每个元素返回 true,否则返回 false.

rfind (a, sub[, start, end])

对于每个元素,返回字符串中找到子字符串 sub 的最高索引,以使 sub 包含在范围 [ start , end ) 中.

rindex (a, sub[, start, end])

rfind 类似,但当找不到子字符串 sub 时,会引发 ValueError .

startswith (a, prefix[, start, end])

返回一个布尔数组,其中 a 中的字符串元素以 prefix 开头的位置为 True ,否则为 False .

str_len (x, /[, out, where, casting, order, ...])

返回每个元素的长度.