This module defines an efficient array of numeric types, mainly including the following types: characters, integers, floating-point numbers. Arrays of numeric types are similar to lists, with the main difference being that lists can be placed into elements of different types, whereas type arrays can only be placed into one type of element. The type array supports only the following types:
Type character |
type C |
Python Type |
Takes up the smallest byte |
Note |
' B ' |
Signed Char |
Int |
1 |
|
B |
unsigned char |
Int |
1 |
|
' U ' |
Py_unicode |
Unicode Haracter |
2 |
|
' H ' |
Signed Short |
Int |
2 |
|
H |
unsigned short |
Int |
2 |
|
I |
Signed int |
Int |
2 |
|
' I ' |
unsigned int |
Int |
2 |
|
L |
Signed Long |
Int |
4 |
|
L |
unsigned long |
Int |
4 |
|
' Q ' |
Signed Long Long |
Int |
8 |
|
' Q ' |
unsigned long long |
Int |
8 |
|
' F ' |
Float |
Float |
4 |
|
' d ' |
Double |
Float |
8 |
|
The actual size of the above type is based on the implementation of the architecture, and more specifically, the implementation of C language.
Class Array.array (typecode[, initializer])
Create a new array using the type code typecode. The parameter initializer is the value that provides the initialization, either a list, a byte array, or an object that can be iterated. If it is a list or string, it will use fromlist (),frombytes (),fromunicode () function to decompose the relevant content and insert it into the array.
Example:
#python 3.4
Import Array
A = Array.array (' l ', [1, 2, 3, 4, 5])
Print (a)
The resulting output is as follows:
Array (' l ', [1, 2, 3, 4, 5])
Array.typecodes
Returns an array that supports all type codes.
Example:
#python 3.4
Import Array
A = Array.array (' l ', [1, 2, 3, 4, 5])
Print (Array.typecodes)
The resulting output is as follows:
Bbuhhiillqqfd
Array.typecode
Returns the type code that created this array.
Example:
#python 3.4
Import Array
A = Array.array (' l ', [1, 2, 3, 4, 5])
Print (A.typecode)
Print (A[0], a[1])
The resulting output is as follows:
L
1 2
Array.itemsize
Internally represents the size of an item that occupies a byte. For example , the whole number of digits is 4 bytes.
Example:
#python 3.4
Import Array
A = Array.array (' l ', [1, 2, 3, 4, 5])
Print (a.itemsize)
Print (A[0:3])
The resulting output is as follows:
4
Array (' l ', [1, 2, 3])
Array.append (x)
Add an item x to the back of the array.
Example:
#python 3.4
Import Array
A = Array.array (' l ', [1, 2, 3, 4, 5])
Print (a.itemsize)
A.append (8)
Print (a)
The resulting output is as follows:
4
Array (' l ', [1, 2, 3, 4, 5, 8])
Array.buffer_info ()
Returns a tuple (memory address, length). Used primarily for compatibility with old C Code, the new code requires the use of the Buffer protocol protocol.
Example:
#python 3.4
Import Array
A = Array.array (' l ', [1, 2, 3, 4, 5])
Print (a.itemsize)
Print (A.buffer_info ())
The resulting output is as follows:
4
(2729208, 5)
Array.byteswap ()
Transform all the elements in the array to "byteswap" in byte order. Only 1,2,4,8 byte sizes are supported. Mainly used to transform the byte order between different machines.
Example:
#python 3.4
Import Array
A = Array.array (' l ', [1, 2, 3, 4, 5])
A.byteswap ()
Print (a)
The resulting output is as follows:
Array (' l ', [16777216, 33554432, 50331648, 67108864, 83886080])
Array.count (x)
Returns the number of occurrences of the specified element x in the array.
Example:
#python 3.4
Import Array
A = Array.array (' l ', [1, 2, 3, 4, 5, 3, 3, 3])
Print (A.count (8))
Print (A.count (3))
The resulting output is as follows:
0
4
Array.extend (iterable)
Adds an iterator to an array of elements in an Iterable object. If iterable is an array, you will throw an exception TypeError if you want to keep the type consistent . If it is not an array type object, you need to be able to convert the type to consistent.
Example:
#python 3.4
Import Array
A = Array.array (' l ', [1, 2, 3, 4, 5])
b = Array.array (' l ', [1, 9, 3])
A.extend (a)
Print (a)
The resulting output is as follows:
Array (' l ', [1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
Array.frombytes (s)
Added from string s to the array.
Array.fromfile (f, N)
reads n entries from the file object F and adds them behind the array. If there are fewer than n entries in the file, exception Eoferror will be thrown , but the available entries will still be added to the array.
Example:
#python 3.4
Import Array
Import Binascii
Import Tempfile
A = Array.array (' I ', Range (5))
Print (' A1: ', a)
Output = Tempfile. Namedtemporaryfile (Delete=false)
A.tofile (Output.file)
Output.flush ()
Name = Output.name
Print (name)
Output.close ()
b = Array.array (' i ')
With open (name, ' RB ') as input:
B.fromfile (input, Len (a))
Print (b)
The resulting output is as follows:
A1:array (' i ', [0, 1, 2, 3, 4])
C:\Users\xxxx\AppData\Local\Temp\tmp_o3px5vs
Array (' i ', [0, 1, 2, 3, 4])
Array.fromlist (list)
Adds an array item from the list. Equivalent to: Forx in List:a.append (x). If the types are not the same, the array contents do not change.
Example:
#python 3.4
Import Array
Import Binascii
Import Tempfile
A = Array.array (' i ')
L = [3, 6, 8, 9]
A.fromlist (L)
Print (a)
The resulting output is as follows:
Array (' I ', [3, 6, 8, 9])
Array.fromstring ()
The functionality of this function has been invalidated. You can use the frombytes () function.
Array.fromunicode (s)
Extends an array from a Unicode string. The array type must be of type Unicode, or the valueerror exception will be thrown .
Example:
#python 3.4
Import Array
A = Array.array (' u ', ' ddd ')
A.fromunicode (' abc ')
Print (a)
The resulting output is as follows:
Array (' u ', ' dddabc ')
Array.index (x)
Returns the smallest index value of the earliest occurrence of element x in an array .
Example:
#python 3.4
Import Array
A = Array.array (' u ', ' 5678ddd ')
Print (A.index (' d '))
The resulting output is as follows:
4
Array.insert (i, X)
Insert an element x before position i . If i is a negative value, the calculation starts at the back of the array.
Example:
#python 3.4
Import Array
A = Array.array (' u ', ' 5678ddd ')
A.insert (3, ' a ')
Print (a)
The resulting output is as follows:
Array (' u ', ' 567a8ddd ')
Array.pop ([i])
Deletes the element at the specified position i , the default value is -1, which means that the last element of the array is deleted.
Example:
#python 3.4
Import Array
A = Array.array (' u ', ' 5678ddd ')
A.pop ()
A.pop (1)
Print (a)
The resulting output is as follows:
Array (' u ', ' 578DD ')
Array.remove (x)
Removes the same value from the array for the first occurrence of element x.
Example:
#python 3.4
Import Array
A = Array.array (' u ', ' 5678ddd ')
A.remove (' 8 ')
Print (a)
The resulting output is as follows:
Array (' u ', ' 567ddd ')
Array.reverse ()
Arranges the elements of an array in reverse order.
Example:
#python 3.4
Import Array
A = Array.array (' u ', ' 5678ddd ')
Print (a)
A.reverse ()
Print (a)
The resulting output is as follows:
Array (' u ', ' 5678ddd ')
Array (' u ', ' ddd8765 ')
Array.tobytes ()
Converts an array to an internal representation of the computer and returns an array of bytes types.
Example:
#python 3.4
Import Array
A = Array.array (' u ', ' 5678ddd ')
Print (A.tobytes ())
The output results are as follows:
B ' 5\x006\x007\x008\x00d\x00d\x00d\x00 '
Array.tofile (f)
Writes the value of all items (as internally represented values) to the file object F.
Example:
#python 3.4
Import Array
Import Binascii
Import Tempfile
A = Array.array (' I ', Range (5))
Print (' A1: ', a)
Output = Tempfile. Namedtemporaryfile (Delete=false)
A.tofile (Output.file)
Output.flush ()
Name = Output.name
Print (name)
Output.close ()
b = Array.array (' i ')
With open (name, ' RB ') as input:
B.fromfile (input, Len (a))
Print (b)
The resulting output is as follows:
A1:array (' i ', [0, 1, 2, 3, 4])
C:\Users\xxxx\AppData\Local\Temp\tmp_o3px5vs
Array (' i ', [0, 1, 2, 3, 4])
Array.tolist ()
Converts an array to a list representation.
Example:
#python 3.4
Import Array
A = Array.array (' u ', ' 5678ddd ')
Print (A.tolist ())
The resulting output is as follows:
[' 5 ', ' 6 ', ' 7 ', ' 8 ', ' d ', ' d ', ' d ']
Array.tostring ()
Converted to a string. This function is going to be discarded and should be used with the tobytes () function.
Array.tounicode ()
Converts an array to a Unicode string.
Example:
#python 3.4
Import Array
A = Array.array (' u ', ' 5678ddd ')
Print (A.tounicode ())
The resulting output is as follows:
5678ddd
Cai Junsheng qq:9073204 Shenzhen
5.6 array--Efficient array of numeric types