Array is not a standard data structure built in Python, but we can also use the array structure in Python if we have an array module. next we will explain how to use the array module in Python.
Initialization
Array instantiation can provide a parameter to describe the data type that is allowed, and an initial data sequence can be stored in the array.
import arrayimport binasciis = 'This is the array.'a = array.array('c', s)print 'As string:', sprint 'As array :', aprint 'As hex :', binascii.hexlify(a)
The array is configured to contain a byte sequence and initialized with a simple string.
>>> ================================ RESTART ================================>>> As string: This is the array.As array : array('c', 'This is the array.')As hex : 54686973206973207468652061727261792e
Process arrays
Similar to other python sequences, you can extend and process arrays in the same way.
import arrayimport pprinta = array.array('i', xrange(3))print 'Initial :', aa.extend(xrange(3))print 'Extended:', aprint 'slice: :', a[2:5]print 'Itetator:'print list(enumerate(a))
Supported operations include sharding, iteration, and adding elements to the end.
>>> ================================ 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)]
Arrays and files
You can use a dedicated built-in method for efficient file reading/writing to write the array content to a file or read the array from the file.
import arrayimport binasciiimport tempfilea = array.array('i', xrange(5))print 'A1: ',aoutput = tempfile.NamedTemporaryFile()a.tofile(output.file)output.flushwith 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
Candidate byte sequence
If the data in the array does not adopt the inherent byte sequence, or the sequence needs to be exchanged before being sent to a system in different byte sequence, you can convert the entire array in python without iteration of each element.
import arrayimport binasciidef 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 () exchanges the byte sequence of elements in the C array, which is more efficient than processing data cyclically in 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