在 Numpy 中返回一個數組的副本,其中每個元素的第一個字元都大寫
要返回一個數組的副本,其中每個元素的第一個字元都大寫,請在 Python Numpy 中使用 **numpy.char.capitalize()** 方法。arr 是要大寫的字串輸入陣列。該函式返回 str 或 unicode 型別的輸出陣列,具體取決於輸入型別。
numpy.char 模組為 numpy.str_ 或 numpy.bytes_ 型別的陣列提供了一組向量化的字串操作。
步驟
首先,匯入所需的庫 -
import numpy as np
建立一個字串的一維陣列 -
arr = np.array(['bella', 'toM', 'john', 'katE', 'amy', 'brad'])
顯示我們的陣列 -
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)要返回一個數組的副本,其中每個元素的第一個字元都大寫,請使用 numpy.char.capitalize() 方法。arr 是要大寫的字串輸入陣列 -
print("
Result (capitalize)...
",np.char.capitalize(arr))
示例
import numpy as np
# Create a One-Dimensional array of string
arr = np.array(['bella', 'toM', 'john', 'katE', 'amy', 'brad'])
# 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("
Elements in the Array...
",arr.size)
# To return a copy of an array with only the first character of each element capitalized, use the numpy.char.capitalize() method in Python Numpy
# The arr is the input array of strings to capitalize
print("
Result (capitalize)...
",np.char.capitalize(arr))輸出
Array... ['bella' 'toM' 'john' 'katE' 'amy' 'brad'] Array datatype... <U5 Array Dimensions... 1 Our Array Shape... (6,) Elements in the Array... 6 Result (capitalize)... ['Bella' 'Tom' 'John' 'Kate' 'Amy' 'Brad']
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP