Open In App

Numpy recarray.byteswap() function | Python

Last Updated : 23 Apr, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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 : Python3
# 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)]

Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy