Skip to content

Latest commit

History

History
48 lines (34 loc) · 1.66 KB

File metadata and controls

48 lines (34 loc) · 1.66 KB

array

一个基础的 Python 内置库,不应该这么晚才发现,看来真的没有将 Python 用到正途上。

array 的功能很简单,很单一,就是创建数组,统一数据类型结构的数组,还能对数组做文件的读写操作,快如闪电。

array 的可以使用的数据类型。

Type codeC TypeMinimum size in bytes
'c'character1
'b'signed integer1
'B'unsigned integer1
'u'Unicode character2
'h'signed integer2
'H'unsigned integer2
'i'signed integer2
'I'unsigned integer2
'l'signed integer4
'L'unsigned integer4
'f'floating point4
'd'floating point8
# coding=utf-8
from array import array
from random import random
floats = array('d', (random() for i in xrange(10**7)))
print floats[-1]
fp = open('floats.bin', 'wb')
floats.tofile(fp)
fp.close()
floats2 = array('d')
fp = open('floats.bin', 'rb')
floats2.fromfile(fp, 10**7)
fp.close()
print floats2[-1]
print floats == floats2