python中分离四位整数_在Python中将32位整数转换为4个8位整数数组

这篇博客介绍了如何在Python中将32位整数转换为4个8位整数,通过使用NumPy的dtype功能创建结构化数组,并通过视图操作进行数据分离。示例代码展示了如何创建和访问这些结构化数组的各个字段,以实现整数的拆分和重组。
摘要由CSDN通过智能技术生成

Subdivide int16 into 2 int8‘s, called x and y. 0 and 1 are the offsets in bytes:

np.dtype((np.int16, {'x':(np.int8,0), 'y':(np.int8,1)}))

dtype(('

或者根据你的情况改编:In [30]: x=np.arange(12,dtype=np.int32)*1000

In [39]: dt=np.dtype((np.int32, {'f0':(np.uint8,0),'f1':(np.uint8,1),'f2':(np.uint8,2), 'f3':(np.uint8,3)}))

In [40]: x1=x.view(dtype=dt)

In [41]: x1['f0']

Out[41]: array([ 0, 232, 208, 184, 160, 136, 112, 88, 64, 40, 16, 248], dtype=uint8)

In [42]: x1['f1']

Out[42]: array([ 0, 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 42], dtype=uint8)

比较In [38]: x%256

Out[38]: array([ 0, 232, 208, 184, 160, 136, 112, 88, 64, 40, 16, 248])2) Tuple argument: The only relevant tuple case that applies to record structures is when a structure is mapped to an existing data type.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值