numpy string operations | translate() function (original) (raw)
Last Updated : 29 Jan, 2019
numpy.core.defchararray.translate(arr, table, deletechars=None)
is another function for doing string operations in numpy. For each element in arr, it 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. If there are more than one values to be translated, a dictionary is passed to maketrans function to create a translation table.
Parameters: arr : array_like of str or unicode.Input array.table : Translate mapping specified to perform translations.deletechars : String type, characters to be deleted. Returns : [ndarray] Output array of str or unicode with translated values.
Code #1 :
Python3 `
Python program explaining
numpy.core.defchararray.translate() method
importing numpy
import numpy as geek
input array
in_arr = geek.array(['Weeks', 'our', 'Weeks']) print ("Input original array : ", in_arr)
creating dictionary for translation table
trans_dict ={"W": "G", "o": "f", "u": "o"}
creating translation table from dictionary
trans_table ="Wou".maketrans(trans_dict)
out_arr = geek.core.defchararray.translate(in_arr, trans_table, deletechars ="None") print ("Output translated array: ", out_arr)
`
Output:
Input original array : ['Weeks' 'our' 'Weeks'] Output translated array: ['Geeks' 'for' 'Geeks']
Similar Reads
- numpy string operations | title() function numpy.core.defchararray.title(arr): function is used to Return element-wise title cased version of string or unicode.Title case words start with uppercase characters, all remaining cased characters are lowercase. Parameters: arr : [ array_like ] Input array which may be str or unicode. Returns : [nd 1 min read
- numpy string operations | strip() function numpy.core.defchararray.strip(arr, chars=None) is another function for doing string operations in numpy. It returns a copy with the leading and trailing characters removed for each element in arr. Parameters: arr : array_like of str or unicode. char : [str or unicode, optional] the set of characters 2 min read
- numpy string operations | swapcase() function numpy.core.defchararray.swapcase(arr) function return element-wise a copy of the string with uppercase characters converted to lowercase and lowercase characters converted to uppercase. Syntax: numpy.char.swapcase(arr) Parameters: arr : [ array_like ] Input array which may be str or unicode. Returns 1 min read
- Numpy - String Functions & Operations NumPy String functions belong to the numpy.char module and are designed to perform element-wise operations on arrays. These functions can help to handle and manipulate string data efficiently. Table of Content String OperationsString Information String Comparison In this article, we’ll explore the v 5 min read
- numpy string operations | zfill() function numpy.core.defchararray.zfill(arr, width) is another function for doing string operations in numpy. For each element in the array it returns the numeric string left-filled with zeros.The number of left filled zeros happen according to the width. Parameters: arr : array_like of str or unicode.Input a 2 min read
- numpy string operations | upper() function numpy.core.defchararray.upper(arr): function is used to return an array with the elements converted to uppercase. Parameters: arr : [ array_like ] Input array which may be str or unicode. Returns : [ndarray] Output uppercased array of str or unicode, depending on input type. Code #1: # Python Progra 1 min read
- numpy string operations | rstrip() function numpy.core.defchararray.rstrip(arr, chars=None) is another function for doing string operations in numpy. It returns a copy with the trailing characters removed for each element in arr. Parameters: arr : array_like of str or unicode. char : [str or unicode, optional] the set of characters to be remo 2 min read
- numpy string operations | rfind() function numpy.core.defchararray.find(arr, sub, start=0, end=None) is another function for doing string operations in numpy.It returns the highest index in the string where substring sub is found for each element in arr.It returns -1 if sub is not contained within [start, end]. Parameters: arr : array_like o 2 min read
- Numpy string operations | rindex() function numpy.core.defchararray.rindex() function, raises ValueError when the substring sub is not found. Calls str.rindex element-wise. Syntax : numpy.core.defchararray.rindex(arr, sub, start = 0, end = None) Parameters : arr : [array-like of str or unicode] Array-like of str . sub : [str or unicode] Input 1 min read
- numpy string operations | join() function numpy.core.defchararray.join(sep, arr) is another function for doing string operations in numpy. For each element in arr, it returns a copy of the string in which the string elements of array have been joined by separator. Parameters: sep : It joins elements with the string between them. arr :Input 1 min read