NumPy: translate() function
numpy.core.defchararray.translate() function
For each element in a given array, the numpy.core.defchararray.translate() function returns a copy of the string where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table.
Calls str.translate element-wise.
Version: 1.15.0
Syntax:
numpy.core.defchararray.translate(a, table, deletechars=None)
Parameter:
Name | Description | Required / Optional |
---|---|---|
a: array-like of str or unicode | Required | |
table: str of length 256 | Required | |
deletechars: str | Optional |
Return value:
out [ndarray] Output array of str or unicode, depending on input type.
Note:
The 'chararray' class exists for backwards compatibility with Numarray, it is not recommended for new development. Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays of 'dtype' 'object_', 'string_' or 'unicode_', and use the free functions in the 'numpy.char' module for fast vectorized string operations.
Some methods will only be available if the corresponding string method is available in your version of Python.
The preferred alias for 'defchararray' is 'numpy.char'.
Example-1: numpy.translate() function
>>> import numpy as np
>>> table = '*'
>>> x = np.char.translate('The quick brown fox', table, deletechars=None)
>>> x
array('The quick brown fox', dtype='<U19')
Example-2: numpy.translate() function
>>> import numpy as np
>>> table = '256'
>>> x = np.char.translate('The quick brown fox jumps over the lazy dog', table, deletechars=None)
>>> x
array('The quick brown fox jumps over the lazy dog', dtype='<U43')
Python - NumPy Code Editor:
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework