在 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']
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP