对于 Numpy 数组中的每个元素,返回一个删除了前导字符和尾随字符的副本
numpyserver side programmingprogramming
要返回一个删除了前导字符和尾随字符的数组副本,请使用 Python Numpy 中的 numpy.char.strip() 方法。"chars" 参数用于设置一个字符串,指定要删除的字符集。如果省略或为 None,chars 参数默认为删除空格。chars 参数不是前缀;而是删除其值的所有组合。
numpy.char 模块为 numpy.str_ 或 numpy.bytes_ 类型的数组提供了一组矢量化字符串操作。
步骤
首先,导入所需的库 −
import numpy as np
创建一个包含一些前导字符和尾随字符的一维字符串数组 −
arr = np.array(['$$Tom$', '$$$$John$$', '$Kate$$', '$Amy$$$$$$$$$', '$Brad$'])
显示我们的数组 −
print("数组...
",arr)
获取数据类型 −
print("
数组数据类型...
",arr.dtype)
获取数组的维度 −
print("
数组维度...
",arr.ndim)
获取数组的形状 −
print("
我们的数组形状...
",arr.shape)
获取数组元素的数量 −
print("
数组中的元素...
",arr.size)
要返回删除了前导和尾随字符的数组副本,请使用 numpy.char.strip() 方法。"chars" 参数用于设置指定要删除的字符集的字符串。如果省略或为 None,chars 参数默认为删除空格。 chars 参数不是前缀;相反,其值的所有组合都会被删除 −
print("
结果...
",np.char.strip(arr, '$'))
示例
import numpy as np # 创建一个包含一些前导和尾随字符的一维字符串数组 arr = np.array(['$$Tom$', '$$$$John$$', '$Kate$$', '$Amy$$$$$$$$$', '$Brad$']) # Displaying our array print("数组...
",arr) # Get the datatype print("
数组数据类型...
",arr.dtype) # 获取数组的维度 print("
数组维度...
",arr.ndim) # 获取数组的形状 print("
我们的数组形状...
",arr.shape) # Get the number of elements of the Array print("
数组中元素的数量...
",arr.size) # 要返回删除了前导和尾随字符的数组副本,请使用 Python Numpy 中的 numpy.char.strip() 方法 # "chars" 参数用于设置指定要删除的字符集的字符串。 # 如果省略或为 None,chars 参数默认为删除空格。 # chars 参数不是前缀;相反,其值的所有组合都会被删除。 print("
结果...
",np.char.strip(arr, '$'))
输出
数组... ['$$Tom$' '$$$$John$$' '$Kate$$' '$Amy$$$$$$$$$' '$Brad$'] 数组数据类型... <U13 数组维度... 1 我们的数组形状... (5,) 数组中元素的数量... 5 结果... ['Tom' 'John' 'Kate' 'Amy' 'Brad']