详解Python中的array数组模块相关使用
作者:lucifercn 时间:2021-03-05 04:04:19
初始化
array实例化可以提供一个参数来描述允许那种数据类型,还可以有一个初始的数据序列存储在数组中。
import array
import binascii
s = 'This is the array.'
a = array.array('c', s)
print 'As string:', s
print 'As array :', a
print 'As hex :', binascii.hexlify(a)
数组配置为包含一个字节序列,用一个简单的字符串初始化。
>>> ================================ RESTART ================================
>>>
As string: This is the array.
As array : array('c', 'This is the array.')
As hex : 54686973206973207468652061727261792e
处理数组
类似于其他python序列,可以采用同样方式扩展和处理array。
import array
import pprint
a = array.array('i', xrange(3))
print 'Initial :', a
a.extend(xrange(3))
print 'Extended:', a
print 'slice: :', a[2:5]
print 'Itetator:'
print list(enumerate(a))
支持的操作包括分片,迭代以及向末尾增加元素。
>>> ================================ RESTART ================================
>>>
Initial : array('i', [0, 1, 2])
Extended: array('i', [0, 1, 2, 0, 1, 2])
slice: : array('i', [2, 0, 1])
Itetator:
[(0, 0), (1, 1), (2, 2), (3, 0), (4, 1), (5, 2)]
数组和文件
可以使用高效读/写文件的专用内置方法将数组的内容写入文件或从文件读取数组。
import array
import binascii
import tempfile
a = array.array('i', xrange(5))
print 'A1: ',a
output = tempfile.NamedTemporaryFile()
a.tofile(output.file)
output.flush
with open(output.name, 'rb') as input:
raw_input = input.read()
print 'Raw Contents:', binascii.hexlify(raw_data)
input.seek(0)
a2 = array.array('i')
a2.fromfile(input, len(a))
print 'A2: ', a2
候选字节顺序
如果数组中的数据没有采用固有的字节顺序,或者在发送到一个采用不同字节顺序的系统前需要交换顺序,可以在python转换整个数组而无须迭代处理每个元素。
import array
import binascii
def to_hex(a):
chars_per_item = a.itemsize * 2
hex_version = binascii.hexlify(a)
num_chunks = len(hex_version) / chars_per_item
for i in xrange(num_chunks):
start = i * chars_per_item
end = start + chars_per_item
yield hex_version[start:end]
a1 = array.array('i', xrange(5))
a2 = array.array('i', xrange(5))
a2.byteswap()
fmt = '%10s %10s %10s %10s'
print fmt % ('A1_hex', 'A1', 'A2_hex', 'A2')
print fmt % (('-' * 10,) * 4)
for value in zip(to_hex(a1), a1, to_hex(a2), a2):
print fmt % value
byteswap()会交换C数组中元素的字节顺序,比在python中循环处理数据高效的多。
>>> ================================ RESTART ================================
>>>
A1_hex A1 A2_hex A2
---------- ---------- ---------- ----------
00000000 0 00000000 0
01000000 1 00000001 16777216
02000000 2 00000002 33554432
03000000 3 00000003 50331648
04000000 4 00000004 67108864
标签:Python,数组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
DOM 精简知识教程
2013-07-18 07:28:33
![](https://img.aspxhome.com/file/UploadPic/20071/200713110268848s.gif)
电子商务搜索LIST页面用户体验设计
2010-08-03 12:57:00
![](https://img.aspxhome.com/file/UploadPic/20108/3/20108313022804s.jpg)
手把手教你利用opencv实现人脸识别功能(附源码+文档)
2022-01-24 23:55:37
![](https://img.aspxhome.com/file/2023/6/93456_0s.jpg)
Python快速查找list中相同部分的方法
2021-01-28 17:26:00
![](https://img.aspxhome.com/file/2023/0/91890_0s.jpg)
python实现人机对战的五子棋游戏
2021-07-05 04:36:03
![](https://img.aspxhome.com/file/2023/0/65910_0s.jpg)
在VScode里面添加Python解释器的详细步骤
2021-12-04 16:03:11
![](https://img.aspxhome.com/file/2023/7/100817_0s.png)
类似google的ASP分页代码[测试通过]
2009-03-13 12:43:00
ASP正则表达式验证域名是否合法
2010-01-02 20:44:00
TensorFlow和Numpy矩阵操作中axis理解及axis=-1的解释
2021-02-14 05:59:09
![](https://img.aspxhome.com/file/2023/3/91933_0s.png)
python基础教程之csv文件的写入与读取
2021-06-18 21:21:00
![](https://img.aspxhome.com/file/2023/4/65524_0s.jpg)
Asp定时执行操作 Asp定时读取数据库(网页定时操作详解)
2011-03-17 11:04:00
pytorch 利用lstm做mnist手写数字识别分类的实例
2023-01-31 03:15:38
CSS改变字体而不影响网页
2010-10-20 20:11:00
JS数组方法汇总
2009-08-03 14:06:00
javascript拼音搜索引擎
2011-08-29 15:42:14
通过启动脚本来感受ASP的力量
2008-11-07 15:25:00
asp随机获取access数据库中的一条记录
2007-08-15 13:11:00
PHP常用函数之获取汉字首字母功能示例
2023-06-16 12:24:39
Python的高级Git库 Gittle
2023-06-14 14:52:05
Numpy 理解ndarray对象的示例代码
2023-07-17 00:56:20
![](https://img.aspxhome.com/file/2023/7/62607_0s.png)