在 NumPy 中返回字串或 Unicode 的元素級標題大小寫版本


要返回字串或 unicode 的元素級標題大小寫版本,請在 Python NumPy 中使用 **numpy.char.title()** 方法。標題大小寫單詞以大寫字元開頭,所有其餘的大小寫字元都為小寫。

函式 title() 返回一個 str 或 unicode 的輸出陣列,具體取決於輸入型別。numpy.char 模組為 numpy.str_ 或 numpy.bytes_ 型別的陣列提供了一組向量化字串操作。

步驟

首先,匯入所需的庫 -

import numpy as np

建立一個字串的一維陣列 -

arr = np.array(['kATIE', 'jOHN', 'Kate', 'AmY', 'brADley'])

顯示我們的陣列 -

print("Array...
",arr)

獲取資料型別 -

print("
Array datatype...
",arr.dtype)

獲取陣列的維度 -

print("
Array Dimensions...
",arr.ndim)

獲取陣列的形狀 -

print("
Our Array Shape...
",arr.shape)

獲取陣列的元素數量 -

print("
Elements in the Array...
",arr.size)

要返回字串或 unicode 的元素級標題大小寫版本,請使用 numpy.char.title() 方法。標題大小寫單詞以大寫字元開頭,所有其餘的大小寫字元都為小寫 -

print("
Result (title case)...
",np.char.title(arr))

示例

import numpy as np

# Create a One-Dimensional array of strings
arr = np.array(['kATIE', 'jOHN', 'Kate', 'AmY', 'brADley'])

# Displaying our array
print("Array...
",arr) # Get the datatype print("
Array datatype...
",arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Number of elements in the Array...
",arr.size) # To return element-wise title cased version of string or unicode, use the numpy.char.title() method in Python Numpy # Title case words start with uppercase characters, all remaining cased characters are lowercase print("
Result (title case)...
",np.char.title(arr))

輸出

Array...
['kATIE' 'jOHN' 'Kate' 'AmY' 'brADley']

Array datatype...
<U7

Array Dimensions...
1

Our Array Shape...
(5,)

Number of elements in the Array...
5

Result (title case)...
['Katie' 'John' 'Kate' 'Amy' 'Bradley']

更新於: 2022-02-21

90 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.