Python中的array数组模块相关使用

时间:2018-01-25关注公众号来源:网络

  初始化

  array实例化可以提供一个参数来描述允许那种数据类型,还可以有一个初始的数据序列存储在数组中。

Python中的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">importarray
importbinASCII
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
importarray
importpprint
a =array.array('i', xrange(3))
print'Initial :', a
a.extend(xrange(3))
print'Extended:', a
print'slice: :', a[2:5]
print'Itetator:'
printlist(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
importarray
importbinascii
importtempfile
 
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转换整个数组而无须迭代处理每个元素。

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
importarray
importbinascii
 
defto_hex(a):
  chars_per_item =a.itemsize *2
  hex_version =binascii.hexlify(a)
  num_chunks =len(hex_version) /chars_per_item
  fori inxrange(num_chunks):
    start =i *chars_per_item
    end =start +chars_per_item
    yieldhex_version[start:end]
 
a1 =array.array('i', xrange(5))
a2 =array.array('i', xrange(5))
a2.byteswap()
 
fmt ='%10s %10s %10s %10s'
printfmt %('A1_hex', 'A1', 'A2_hex', 'A2')
printfmt %(('-'*10,) *4)
forvalue inzip(to_hex(a1), a1, to_hex(a2), a2):
  printfmt %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

阅读全文
扫码关注“ 多特资源库
更多更全的软件资源下载
文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站)
玩家热搜

相关攻略

正在加载中
版权
版权说明

文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站)

电话:13918309914

QQ:1967830372

邮箱:[email protected]

toast