The array
module implements an efficient array storage type. Arrays are
similar to lists, but all items must be of the same primitive type.
The type is defined when the array is created.
Examples 4-1 through 4-5 are simple ones. Example 4-1 creates an
array
object and copies the internal buffer to
a string through the tostring
method.
Example 4-1. Using the array Module to Convert Lists of Integers to Strings
File: array-example-1.py import array a = array.array("B", range(16)) # unsigned char b = array.array("h", range(16)) # signed short print a print repr(a.tostring()) print b print repr(b.tostring())array('B', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
' 00 01 02 03 04 05 06 07 10 11 12 13 14 15 16 17'
array('h', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
' 00 00 01 00 02 00 03 00 04 00 05 00 06 00 07 00