For an array of arrays in Python use _python

Source: Internet
Author: User
Tags arrays extend in python

Class
Array instantiation can provide a parameter to describe the type of data that is allowed, and an initial sequence of data can be stored in an array.

Import array
Import binascii
s = ' This is the array. '
A = Array.array (' C ', s)
print ' As String: ', s
print ' as array: ', a
print ' 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 process array in the same way.

Import array
import pprint
a = Array.array (' i ', xrange (3))
print ' Initial: ', a
a.extend (xrange (3)
print ' Extended: ', a
print ' slice:: ', A[2:5]
print ' itetator: '
Print List (enumerate (a))

Supported operations include fragmentation, 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])
Itetat Or:
[(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 to read an array from a file.

Import array
import binascii
import tempfile

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

Candidate byte order
if the data in the array does not have an intrinsic byte order, or if you need to exchange order before sending to a system that takes a different byte order, you can convert the entire array in Python without iterating through each element.

Import array
import BINASCII

def 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 * C Hars_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 '
print%10s% (' fmt ', ' A1_hex ', ' A2_hex ', ' A2 ')
print fmt% (('-' *,) * 4) for
value in Zip (To_hex (A1), A1, To_hex (A2), A2):
  Print FM T% value

Byteswap () swaps the byte order of elements in the C array, much more efficiently than iterating through 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.