对于 NumPy 数组中的每个元素,返回一个删除前导空格的副本。
要返回一个删除前导空格的数组副本,请在 Python NumPy 中使用 **numpy.char.lstrip()** 方法。该函数根据输入类型返回 str 或 unicode 的输出数组。
numpy.char 模块为 numpy.str_ 或 numpy.bytes_ 类型的数组提供了一组矢量化字符串操作。
chars 参数是一个字符串,指定要删除的字符集。如果省略或为 None,则 chars 参数默认为删除空格。chars 参数不是前缀;相反,删除其所有值的组合。
步骤
首先,导入所需的库 -
import numpy as np
创建一个包含一些前导和尾随空格的字符串的一维数组 -
arr = np.array([' 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.lstrip() 方法 -
print("
Result...
",np.char.lstrip(arr))
示例
import numpy as np
# Create a One-Dimensional array of string with some leading and trailing spaces
arr = np.array([' 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("
Number of elements in the Array...
",arr.size)
# To return a copy of an array with the leading spaces removed, use the numpy.char.lstrip() method in Python Numpy
print("
Result...
",np.char.lstrip(arr))输出
Array... [' Tom ' ' John ' ' Kate ' ' Amy' 'Brad '] Array datatype... <U6 Array Dimensions... 1 Our Array Shape... (5,) Number of elements in the Array... 5 Result... ['Tom ' 'John ' 'Kate ' 'Amy' 'Brad ']
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP