Skip to content
Related Articles

Related Articles

How to split the element of a given NumPy array with spaces?

View Discussion
Improve Article
Save Article
  • Last Updated : 25 Oct, 2020
View Discussion
Improve Article
Save Article

To split the elements of a given array with spaces we will use numpy.char.split(). It is a function for doing string operations in NumPy. It returns a list of the words in the string, using sep as the delimiter string for each element in arr.

Parameters:
arr : array_like of str or unicode.Input array.
sep : [ str or unicode, optional] specifies the separator to use when splitting the string.
maxsplit : how many maximum splits to do.

Returns : [ndarray] Output Array containing of list objects.

Example 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)

Output :

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

Example 2:

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)

Output: 

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

Example 3:

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)

Output:

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!