字符串功能#

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])

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

lower (a)

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

lstrip (a[, chars])

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

mod (a, values)

返回 (a % i),即 pre-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])

返回一个数组,其中 a 的元素在长度为 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, ...])

返回每个元素的长度.