Numpy recarray.byteswap() function | Python
In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)]
, where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']
.
Record arrays allow the fields to be accessed as members of the array, using arr.a and arr.b
. numpy.recarray.byteswap()
function Swap the bytes of the array elements.
Syntax :
numpy.recarray.byteswap(inplace=False)
Parameters:
inplace : [bool, optional] If True, swap bytes in-place, default is False.Return : [ndarray] byteswapped array. If inplace is True, this is a view to self.
Code :
# Python program explaining # numpy.recarray.byteswap() method # importing numpy as geek import numpy as geek # creating input array with 2 different field in_arr = geek.array([( 5.0 , 2 ), ( 3.0 , - 4 ), ( 6.0 , 9 )], dtype = [( 'a' , float ), ( 'b' , int )]) print ( "Input array : " , in_arr) # convert it to a record array, # using arr.view(np.recarray) rec_arr = in_arr.view(geek.recarray) # applying recarray.byteswap methods to record array out_arr = rec_arr.byteswap() print ( "Output swapped record array : " , out_arr) |
Output:
Input array : [(5.0, 2) (3.0, -4) (6.0, 9)] Output swapped record array : [(2.561e-320, 144115188075855872) (1.0435e-320, -216172782113783809) (3.067e-320, 648518346341351424)]
Contact Us