在NumPy数组中,为每个元素返回一个副本,其中去掉了前导字符
要返回一个去掉了前导字符的数组副本,请使用Python NumPy中的**numpy.char.lstrip()**方法。"chars"参数用于设置一个字符串,指定要删除的字符集。如果省略或为None,则chars参数默认为删除空格。chars参数不是前缀;而是删除其所有值的组合。
numpy.char模块提供了一组针对numpy.str_或numpy.bytes_类型数组的矢量化字符串操作。
chars参数是一个字符串,指定要删除的字符集。如果省略或为None,则chars参数默认为删除空格。chars参数不是前缀;而是删除其所有值的组合。
步骤
首先,导入所需的库:
import numpy as np
创建一个包含一些前导和尾随字符的一维字符串数组:
arr = np.array(['zzzzTomzzzz', 'zzzJohn', 'zKate ', 'zAmyzz', 'zBradz'])
显示我们的数组:
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, 'z'))
示例
import numpy as np
# Create a One-Dimensional array of string with some leading and trailing characters
arr = np.array(['zzzzTomzzzz', 'zzzJohn', 'zKate ', 'zAmyzz', 'zBradz'])
# 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 characters removed, use the numpy.char.lstrip() method in Python Numpy
# The "chars" parameter is used to set a string specifying the set of characters to be removed.
# If omitted or None, the chars argument defaults to removing whitespace.
# The chars argument is not a prefix; rather, all combinations of its values are stripped.
print("
Result...
",np.char.lstrip(arr, 'z'))输出
Array... ['zzzzTomzzzz' 'zzzJohn' 'zKate ' 'zAmyzz' 'zBradz'] Array datatype... <U11 Array Dimensions... 1 Our Array Shape... (5,) Number of elements in the Array... 5 Result... ['Tomzzzz' 'John' 'Kate ' 'Amyzz' 'Bradz']
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP