NumPy - 字串函式



NumPy 中的字串函式旨在對字串陣列進行操作。它們是 NumPy 的char模組的一部分,該模組提供了一組向量化字串操作,可應用於字串陣列的每個元素。

NumPy 字串函式的關鍵特性

以下是 NumPy 字串函式的關鍵特性:

  • 元素級操作:NumPy 字串函式的核心優勢在於能夠獨立地對陣列的每個元素執行操作。這允許有效地操作大型資料集。
  • 向量化:透過使用 NumPy 庫,操作被向量化,即與傳統的 Python 字串處理方法相比,它提高了效能。向量化利用最佳化的 C 庫來執行計算,從而顯著減少執行時間。
  • 與陣列的相容性:NumPy 字串函式直接與字串陣列一起使用,從而可以更輕鬆地處理大量文字資料,而無需將其轉換為列表或其他格式。

字串操作在陣列上按元素執行。它們特別適用於低階資料操作和高效計算。

字串函式列表

以下函式用於對 dtype 為 numpy.string_ 或 numpy.unicode_ 的陣列執行向量化字串操作。它們基於 Python 內建庫中的標準字串函式。

序號 操作和描述
1 numpy.char.add()

按元素連線兩個字串陣列。

2 numpy.char.center()

將陣列中每個字串居中於指定寬度內,並使用指定字元填充。

3 numpy.char.capitalize()

將陣列中每個字串的第一個字元大寫。

4 numpy.char.decode()

使用指定的編碼解碼陣列中的每個字串。

5 numpy.char.encode()

使用指定的編碼編碼陣列中的每個字串。

6 numpy.char.ljust

左對齊陣列中的每個字串,並使用指定字元填充。

7 numpy.char.lower()

將陣列中每個字串的所有字元轉換為小寫。

8 numpy.char.lstrip()

去除陣列中每個字串開頭的字元。

9 numpy.char.mod()

使用字串中佔位符的指定值格式化字串。

10 numpy.char.multiply()

將陣列中每個字串重複指定次數。

11 numpy.char.replace()

將每個字串中子字串的出現次數替換為另一個子字串。

12 numpy.char.rjust()

右對齊陣列中的每個字串,並使用指定字元填充。

13 numpy.char.rstrip()

去除陣列中每個字串末尾的字元。

14 numpy.char.strip()

去除陣列中每個字串開頭和末尾的字元。

15 numpy.char.swapcase()

交換每個字串中每個字元的大小寫。

16 numpy.char.title()

將陣列中的每個字串轉換為標題大小寫。

17 numpy.char.translate()

根據轉換錶轉換每個字串中的字元。

18 numpy.char.upper()

將陣列中每個字串的所有字元轉換為大寫。

19 numpy.char.zfill()

在每個字串的左側填充零,以填充指定的寬度。

20 numpy.char.equal()

比較陣列中每個字串與另一個數組的相等性。

21 numpy.char.not_equal()

比較陣列中每個字串與另一個數組的不相等性。

22 numpy.char.greater_equal()

比較陣列中每個字串是否大於或等於另一個。

23 numpy.char.less_equal()

比較陣列中每個字串是否小於或等於另一個。

24 numpy.char.greater()

比較陣列中每個字串是否大於另一個。

25 numpy.char.less()

比較陣列中每個字串是否小於另一個。

26 numpy.char.count()

計算陣列中每個字串中子字串出現的次數。

27 numpy.char.endswith()

檢查陣列中每個字串是否以指定的字尾結尾。

28 numpy.char.find()

查詢每個字串中子字串的最低索引。

29 numpy.char.index()

類似於 find,但如果未找到子字串則引發錯誤。

30 numpy.char.isalnum()

檢查每個字串是否為字母數字。

31 numpy.char.isalpha()

檢查每個字串是否為字母。

32 numpy.char.isdecimal()

檢查每個字串是否為十進位制字串。

33 numpy.char.isdigit

檢查每個字串是否僅包含數字。

34 numpy.char.islower()

檢查每個字串是否為小寫。

35 numpy.char.isnumeric()

檢查每個字串是否為數字。

36 numpy.char.isspace()

檢查每個字串是否僅包含空格。

37 numpy.char.istitle()

檢查每個字串是否為標題大小寫。

38 numpy.char.isupper()

檢查每個字串是否為大寫。

39 numpy.char.rfind()

查詢每個字串中子字串的最高索引。

40 numpy.char.rindex()

類似於 rfind,但如果未找到子字串則引發錯誤。

41 numpy.char.startswith()

檢查每個字串是否以指定的字首開頭。

42 numpy.char.str_len()

返回陣列中每個字串的長度。

43 numpy.char.split()

返回分割後的陣列字串。

44 numpy.char.splitlines()

將字串陣列的每個元素拆分為一個行列表。

45 numpy.char.join()

使用指定的定界符連線字串陣列的元素。

讓我們快速瞭解一下重要的函式:

add() 函式

NumPy 中的 add() 函式用於使用+運算子連線字串,如下例所示:

a = "Hello"
b = "World"
result = a + " " + b
print(result)

獲得以下輸出:

Hello World

multiply() 函式

NumPy 中的 multiply() 函式用於使用*運算子乘法(重複)字串,如下例所示:

a = "Hello"
result = a * 3
print(result)

這將產生以下結果:

HelloHelloHello

center() 函式

center() 函式將字串居中於指定寬度的欄位中,並使用空格或指定字元填充:

s = "hello"
result = s.center(10, '*')
print(result)

以上程式碼的輸出如下:

**hello***

capitalize() 函式

capitalize() 函式將字串的第一個字元大寫,並將所有其他字元轉換為小寫:

s = "hello world"
result = s.capitalize()
print(result)

獲得的輸出如下所示:

Hello world

title() 函式

title() 函式將字串中每個單詞的第一個字母大寫:

s = "hello world"
result = s.title()
print(result)

執行以上程式碼後,我們將獲得以下輸出:

Hello World

lower() 和 upper() 函式

lower() 函式將字串中的所有字元轉換為小寫。而 upper() 函式將字串中的所有字元轉換為大寫:

s = "Hello World"
res1 = s.lower()
res2 = s.upper()
print("Lowercase:", res1)
print("Uppercase:",res2)

產生的結果如下:

Lowercase: hello world
Uppercase: HELLO WORLD

decode() 函式

在 Python 3 中,decode() 函式通常用於位元組物件,而不是字串。要將位元組解碼為字串,您使用 decode() 函式:

# Bytes object
b = b"hello world"
result = b.decode('utf-8')
print(result)

我們得到以下輸出:

hello world
廣告