初始化
array实例化可以提供一个参数来描述允许那种数据类型,还可以有一个初始的数据序列存储在数组中。
?
1 2 3 4 5 6 7 | Stream Vera Sans Mono', 'Courier New', Courier, monospace !important; float: none !important; border-top-width: 0px !important; border-bottom-width: 0px !important; height: auto !important; color: rgb(0, 102, 153) !important; vertical-align: baseline !important; overflow: visible !important; top: auto !important; right: auto !important; font-weight: bold !important; left: auto !important; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;" class="py keyword">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) |
数组配置为包含一个字节序列,用一个简单的字符串初始化。
?
1 2 3 4 5 | >>> ================================ RESTART
================================ >>> As string: This is the array. As array : array('c', 'This is the array.') As hex :
54686973206973207468652061727261792e |
处理数组
类似于其他Python序列,可以采用同样方式扩展和处理array。
?
1 2 3 4 5 6 7 8 9 | 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)) |
支持的操作包括分片,迭代以及向末尾增加元素。
?
1 2 3 4 5 6 7 | >>> ================================ 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)] |
数组和文件
可以使用高效读/写文件的专用内置方法将数组的内容写入文件或从文件读取数组。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import array import binascii import tempfile a = array.array( 'i' , xrange ( 5 )) print 'A1: ' ,a 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转换整个数组而无须迭代处理每个元素。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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 ) print fmt % value |
byteswap()会交换C数组中元素的字节顺序,比在python中循环处理数据高效的多。
?
1 2 3 4 5 6 7 8 9 | >>> ================================ 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 |