在 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