Python Numpy recarray.bytewap()函数

Python Numpy recarray.bytewap()函数

在numpy中,数组可以有一个包含字段的数据类型,类似于电子表格中的列。一个例子是[(a, int), (b, float)] ,其中数组中的每个条目是一对(int, float)。通常情况下,这些属性使用字典查询,如arr[‘a’] 和arr[‘b’] 。

记录数组允许字段作为数组的成员被访问,使用arr.a和arr.b . numpy.recarray.byteswap() 函数交换数组元素的字节。

语法: numpy.recarray.byteswap(inplace=False)

参数:
inplace : [bool, optional] 如果为真,就地交换字节,默认为假。

返回: [ndarray] 字节交换的数组。如果inplace是True,这就是一个对自己的视图。

代码 :

# 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) 

输出:

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)]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Numpy 数组操作