Numpy recarray.byteswap() function | Python
Last Updated :
23 Apr, 2019
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 :
import numpy as geek
in_arr = geek.array([( 5.0 , 2 ), ( 3.0 , - 4 ), ( 6.0 , 9 )],
dtype = [( 'a' , float ), ( 'b' , int )])
print ( "Input array : " , in_arr)
rec_arr = in_arr.view(geek.recarray)
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)]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...