NumPy String: numpy.char.rsplit() function
numpy.char.rsplit() function
For each element in a given array the numpy.char.rsplit() function returns a list of the words in the string, using separator as the delimiter string, working from the end of the string towards the beginning.
The function is useful for splitting file paths or filename.
Syntax:
numpy.char.rsplit(a, sep=None, maxsplit=None)
Parameters:
Name | Description | Required / Optional |
---|---|---|
a | array-like of string or unicode | Required |
sep: str or unicode | If sep is not specified or None, any whitespace string is a separator. | Optional |
maxsplit: int | If maxsplit is given, at most maxsplit splits are done, the rightmost ones. | Optional |
Return value:
out : ndarray - Array of list objects
Example: Using numpy.char.rsplit() to split a string at specified separator
>>> import numpy as np
>>> x = np.char.rsplit('w3resource', ':', maxsplit=None)
>>> x
array(list(['w3resource']), dtype=object)
In the above code, the string 'w3resource' is split using ':' as the separator. Since ':' is not present in the string, the original string is returned as a list with one element. The maxsplit parameter is set to None, indicating that the split should continue until there are no more separators in the string.
Example: Splitting a string using numpy.char.rsplit()
>>> import numpy as np
>>> y = np.char.rsplit('hello world', ':', maxsplit=11)
>>> y
array(list(['hello world']), dtype=object)
In the above case, the string "hello world" is split using the delimiter ":". Since there are no instances of the delimiter in the string, the function returns an array with a single element, which is the original string "hello world". The maxsplit parameter is set to 11, which means that the string is split at most 11 times. Since there is only one element in the string, this parameter has no effect in this case
Pictorial Presentation:
Python - NumPy Code Editor:
Previous:
rpartition()
Next:
rstrip()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/numpy/string-operations/rsplit.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics