字串功能#

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

傳回每個字串元素的副本,其中所有 Tab 字元都替換為一個或多個空格。

ljust(a, width[, fillchar])

傳回一個陣列,其元素在長度為 width 的字串中靠左對齊。

lower(a)

傳回一個陣列,其中的元素已轉換為小寫。

lstrip(a[, chars])

對於 a 中的每個元素,傳回一個已移除開頭字元的副本。

mod(a, values)

傳回 (a % i),即 pre-Python 2.6 字串格式化(內插),適用於一對 str 或 unicode 的 array_like 物件。

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 中的每個元素,傳回一個已移除結尾字元的副本。

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

傳回每個元素的長度。