Movatterモバイル変換
[0]ホーム
This module defines an 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' | char | character | 1 |
'b' | signed char | int | 1 |
'B' | unsigned char | int | 1 |
'u' | Py_UNICODE | Unicode character | 2 |
'h' | signed short | int | 2 |
'H' | unsigned short | int | 2 |
'i' | signed int | int | 2 |
'I' | unsigned int | long | 2 |
'l' | signed long | int | 4 |
'L' | unsigned long | long | 4 |
'f' | float | float | 4 |
'd' | double | float | 8 |
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 type:
| array( | typecode[, initializer]) |
- Return a new array whose items are restricted bytypecode,and initialized from the optionalinitializer value, whichmust be a list, string, or iterable over elements of theappropriate type.Changed in version 2.4:Formerly, only lists or strings were accepted.If given a list or string, the initializer is passed to thenew array'sfromlist(),fromstring(), orfromunicode() method (see below) to add initial items tothe array. Otherwise, the iterable initializer is passed to theextend() method.
- ArrayType
- Obsolete alias forarray.
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.
The following data items and methods are also supported:
- typecode
- The typecode character used to create the array.
- itemsize
- The length in bytes of one array item in the internal representation.
- Append a new item with valuex to the end of the array.
- 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'' 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.
- Return the number of occurrences ofx in the array.
- Append items fromiterable to the end of the array. Ifiterable is another array, it must haveexactly the sametype code; if not,TypeError will be raised. Ifiterable is not an array, it must be iterable and itselements must be the right type to be appended to the array.Changed in version 2.4:Formerly, the argument could only be another array.
- 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.
- 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.
- Appends items from the string, interpreting the string as anarray of machine values (as if it had been read from afile using thefromfile() method).
- Extends this array with data from the given unicode string. The arraymust be a type
'u' array; otherwise aValueErroris raised. Use "array.fromstring(ustr.decode(enc))" toappend Unicode data to an array of some other type.
- Return the smallesti such thati is the index ofthe first occurrence ofx in the array.
- Insert a new item with valuex in the array before positioni. Negative values are treated as being relative to the endof the array.
- 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.
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 the first occurrence ofx from the array.
- Reverse the order of the items in the array.
- Write all items (as machine values) to the file objectf.
- Convert the array to an ordinary list with the same items.
- 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.)
- Convert the array to a unicode string. The array must bea type
'u' array; otherwise aValueError is raised.Use "array.tostring().decode(enc)" to obtain a unicode stringfrom an array of some other type.
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('u', u'hello \textbackslash u2641')array('l', [1, 2, 3, 4, 5])array('d', [1.0, 2.0, 3.14])
Release 2.5.2, documentation updated on 21st February, 2008. SeeAbout this document... for information on suggesting changes.
[8]ページ先頭