Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:5.8.1 ExampleUp:5. Miscellaneous ServicesNext:5.10 ConfigParser

5.9array -- Efficient arrays of numeric values

This module defines a new object type which can efficiently representan array of basic values: characters, integers, floating pointnumbers. Arrays are sequence types and behave very muchlike lists, except that the type of objects stored in them isconstrained. The type is specified at object creation time by using atype code, which is a single character. The following typecodes are defined:

Type code C Type Python Type Minimum size in bytes 
'c'charcharacter1
'b'signed charint1
'B'unsigned charint1
'h'signed shortint2
'H'unsigned shortint2
'i'signed intint2
'I'unsigned intlong2
'l'signed longint4
'L'unsigned longlong4
'f'floatfloat4
'd'doublefloat8

The actual representation of values is determined by the machinearchitecture (strictly speaking, by the C implementation). The actualsize can be accessed through theitemsize attribute. The valuesstored for'L' and'I' items will be represented asPython long integers when retrieved, because Python's plain integertype cannot represent the full range of C's unsigned (long) integers.

The module defines the following function and type object:

array(typecode[, initializer])
Return a new array whose items are restricted bytypecode, andinitialized from the optionalinitializer value, which must be alist or a string. The list or string is passed to the new array'sfromlist() orfromstring() method (see below) to addinitial items to the array.

ArrayType
Type object corresponding to the objects returned byarray().

Array objects support the ordinary sequence operations ofindexing, slicing, concatenation, and multiplication. When usingslice assignment, the assigned value must be an array object with thesame type code; in all other cases,TypeError is raised.Array objects also implement the buffer interface, and may be usedwherever buffer objects are supported.

Array objects support the following data items and methods:

typecode
The typecode character used to create the array.

itemsize
The length in bytes of one array item in the internal representation.

append(x)
Append a new item with valuex to the end of the array.

buffer_info()
Return a tuple(address,length) giving the currentmemory address and the length in elements of the buffer used to holdarray's contents. The size of the memory buffer in bytes can becomputed asarray.buffer_info()[1] *array.itemsize. This is occasionally useful when working withlow-level (and inherently unsafe) I/O interfaces that require memoryaddresses, such as certainioctl() operations. Thereturned numbers are valid as long as the array exists and nolength-changing operations are applied to it.

Note:When using array objects from code written in C orC++ (the only way to effectively make use of this information), itmakes more sense to use the buffer interface supported by arrayobjects. This method is maintained for backward compatibility andshould be avoided in new code. The buffer interface is documented inthePython/C API Reference Manual.

byteswap()
``Byteswap'' all items of the array. This is only supported forvalues which are 1, 2, 4, or 8 bytes in size; for other types ofvalues,RuntimeError is raised. It is useful when readingdata from a file written on a machine with a different byte order.

count(x)
Return the number of occurences ofx in the array.

extend(a)
Append array items froma to the end of the array. The twoarrays must haveexactly the same type code; if not,TypeError will be raised.

fromfile(f, n)
Readn items (as machine values) from the file objectfand append them to the end of the array. If less thann itemsare available,EOFError is raised, but the items that wereavailable are still inserted into the array.f must be a realbuilt-in file object; something else with aread() method won'tdo.

fromlist(list)
Append items from the list. This is equivalent to"for x inlist: a.append(x)"except that if there is a type error, the array is unchanged.

fromstring(s)
Appends items from the string, interpreting the string as anarray of machine values (as if it had been read from afile using thefromfile() method).

index(x)
Return the smallesti such thati is the index ofthe first occurence ofx in the array.

insert(i, x)
Insert a new item with valuex in the array before positioni.

pop([i])
Removes the item with the indexi from the array and returnsit. The optional argument defaults to-1, so that by defaultthe last item is removed and returned.

read(f, n)
Deprecated since release 1.5.1.Use thefromfile() method.

Readn items (as machine values) from the file objectfand append them to the end of the array. If less thann itemsare available,EOFError is raised, but the items that wereavailable are still inserted into the array.f must be a realbuilt-in file object; something else with aread() method won'tdo.

remove(x)
Remove the first occurence ofx from the array.

reverse()
Reverse the order of the items in the array.

tofile(f)
Write all items (as machine values) to the file objectf.

tolist()
Convert the array to an ordinary list with the same items.

tostring()
Convert the array to an array of machine values and return thestring representation (the same sequence of bytes that wouldbe written to a file by thetofile() method.)

write(f)
Deprecated since release 1.5.1.Use thetofile() method.

Write all items (as machine values) to the file objectf.

When an array object is printed or converted to a string, it isrepresented asarray(typecode,initializer). Theinitializer is omitted if the array is empty, otherwise it is astring if thetypecode is'c', otherwise it is a list ofnumbers. The string is guaranteed to be able to be converted back toan array with the same type and value using reverse quotes(``), so long as thearray() function has beenimported usingfrom array import array. Examples:

array('l')array('c', 'hello world')array('l', [1, 2, 3, 4, 5])array('d', [1.0, 2.0, 3.14])

See Also:

Modulestruct:
Packing and unpacking of heterogeneous binary data.
Modulexdrlib:
Packing and unpacking of External Data Representation (XDR) data as used in some remote procedure call systems.
The Numerical Python Manual
The Numeric Python extension (NumPy) defines another array type; seehttp://numpy.sourceforge.net/ for further information about Numerical Python. (A PDF version of the NumPy manual is available athttp://numpy.sourceforge.net/numdoc/numdoc.pdf.)


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:5.8.1 ExampleUp:5. Miscellaneous ServicesNext:5.10 ConfigParser
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp