Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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:

Previous: title()
Next: upper()