在NumPy中使用分隔符作為分隔符字串返回字串中的單詞列表
要使用分隔符作為分隔符字串返回字串中的單詞列表,請在 Python NumPy 中使用 **numpy.char.split()** 方法:
- 第一個引數是輸入陣列
- 第二個引數是分隔符
如果給出 maxsplit 引數,則最多進行 maxsplit 次分割。split() 函式返回一個列表物件陣列。
numpy.char 模組為 numpy.str_ 或 numpy.bytes_ 型別陣列提供了一組向量化字串操作。
步驟
首先,匯入所需的庫:
import numpy as np
建立一個數組:
arr = np.array(["Bella-Cio", "Brad-Pitt", "Katie-Perry"])
顯示我們的陣列:
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.split() 方法:
print("
Result...
",np.char.split(arr, '-'))
示例
import numpy as np
# Create an array
arr = np.array(["Bella-Cio", "Brad-Pitt", "Katie-Perry"])
# 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 list of the words in the string using separator as the delimiter string, use the numpy.char.split() method in Python Numpy
# The 1st parameter is the input array
# The 2nd parameter is the separator
print("
Result...
",np.char.split(arr, '-'))輸出
Array... ['Bella-Cio' 'Brad-Pitt' 'Katie-Perry'] Array datatype... <U11 Array Dimensions... 1 Our Array Shape... (3,) Elements in the Array... 3 Result... [list(['Bella', 'Cio']) list(['Brad', 'Pitt']) list(['Katie', 'Perry'])]
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP