A detailed description of the array array module used in Python

Source: Internet
Author: User
Initialization
An array instantiation can provide a parameter to describe which data type is allowed, and an initial data sequence can be stored in an 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 sequence of bytes initialized with a simple string.

>>> ================================ RESTART ================================>>> as String:this is The array. As Array:array (' C ', ' This is the array. ') As Hex  : 54686973206973207468652061727261792e


Working with arrays
Similar to other Python sequences, you can extend and work with 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, iterating, 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 of efficient read/write files to write the contents of an array to a file or read an array from a 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 order
If the data in the array does not have an intrinsic byte order, or if a swap order is required before sending to a system that takes a different byte order, the entire array can be transformed in Python without having to iterate over 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    E nd = 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% (('-' * ' *) * 4) for VA Lue in Zip (To_hex (A1), A1, To_hex (A2), A2):  Print FMT% value

Byteswap () swaps the byte order of the elements in the C array, which is much more efficient than looping the data 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
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.