在NumPy中返回每个字符串元素的副本,其中所有制表符都替换为空格
要返回每个字符串元素的副本,其中所有制表符都替换为空格,请使用Python NumPy中的**numpy.char.expandtabs()**方法。我们还可以设置“**tabsize**”参数,即用tabsize个空格替换制表符。如果未给出,则默认为8个空格。
expandtabs()函数返回每个字符串元素的副本,其中所有制表符都替换为一个或多个空格,具体取决于当前列和给定的tabsize。在字符串中出现每个换行符后,列号将重置为零。它不理解其他非打印字符或转义序列。
numpy.char模块为numpy.str_或numpy.bytes_类型的数组提供了一组矢量化字符串操作。
步骤
首先,导入所需的库:
import numpy as np
创建一个一维字符串数组:
arr = np.array(['Bella\tCio', 'Tom\tHanks', 'Monry\tHeist\tSeries'])
显示我们的数组:
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.expandtabs()方法。我们还可以设置“tabsize”参数,即用tabsize个空格替换制表符。如果未给出,则默认为8个空格:
print("
Result (expand tabs)...
",np.char.expandtabs(arr))
示例
import numpy as np
# Create a One-Dimensional array of string
arr = np.array(['Bella\tCio', 'Tom\tHanks', 'Monry\tHeist\tSeries'])
# 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 each string element where all tab characters are replaced by spaces, use the numpy.char.expandtabs() method in Python Numpy
# We can also set the "tabsize" parameter i.e. replace tabs with tabsize number of spaces. If not given defaults to 8 spaces.
print("
Result (expand tabs)...
",np.char.expandtabs(arr))输出
Array... ['Bella\tCio' 'Tom\tHanks' 'Monry\tHeist\tSeries'] Array datatype... <U18 Array Dimensions... 1 Our Array Shape... (3,) Elements in the Array... 3 Result (expand tabs)... ['Bella Cio' 'Tom Hanks' 'Monry Heist Series']
广告
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP