📜  如何用空格分割给定 NumPy 数组的元素?

📅  最后修改于: 2022-05-13 01:55:25.999000             🧑  作者: Mango

如何用空格分割给定 NumPy 数组的元素?

要使用空格分割给定数组的元素,我们将使用 numpy.char.split()。它是在 NumPy 中进行字符串操作的函数。它返回字符串中单词的列表,使用 sep 作为 arr 中每个元素的分隔符字符串。

示例 1:

Python3
import numpy as np
  
  
# Original Array
array = np.array(['PHP C# Python C Java C++'], dtype=np.str)
print(array)
  
# Split the element of the said array with spaces
sparr = np.char.split(array)
print(sparr)


Python3
import numpy as np
  
  
# Original Array
array = np.array(['Geeks For Geeks'], dtype=np.str)
print(array)
  
# Split the element of the said array 
# with spaces
sparr = np.char.split(array)
print(sparr)


Python3
import numpy as np
  
  
# Original Array
array = np.array(['DBMS OOPS DS'], dtype=np.str)
print(array)
  
# Split the element of the said array 
# with spaces
sparr = np.char.split(array)
print(sparr)


输出

['PHP C# Python C Java C++']
[list(['PHP', 'C#', 'Python', 'C', 'Java', 'C++'])]

示例 2:

蟒蛇3

import numpy as np
  
  
# Original Array
array = np.array(['Geeks For Geeks'], dtype=np.str)
print(array)
  
# Split the element of the said array 
# with spaces
sparr = np.char.split(array)
print(sparr)

输出:

['Geeks For Geeks']
[list(['Geeks', 'For', 'Geeks'])]

示例 3:

蟒蛇3

import numpy as np
  
  
# Original Array
array = np.array(['DBMS OOPS DS'], dtype=np.str)
print(array)
  
# Split the element of the said array 
# with spaces
sparr = np.char.split(array)
print(sparr)

输出:

['DBMS OOPS DS']
[list(['DBMS', 'OOPS', 'DS'])]