如果我们只需要一个包含数字的列表,那么array.array比list更高效。 In [ 25 ] : import array In [ 26 ] : help ( array ) Help on built-in module array : NAME array DESCRIPTION This module defines an object type which can
如果我们只需要一个包含数字的列表,那么array.array比list更高效。
In [25]: import arrayIn [26]: help(array)
Help on built-in module array:
NAME
array
DESCRIPTION
This module defines an object type which can efficiently represent
an array of basic values: characters, integers, floating point
numbers. Arrays are sequence types and behave very much like lists,
except that the type of objects stored in them is constrained.
CLASSES
builtins.object
array
ArrayType = class array(builtins.object)
| array(typecode [, initializer]) -> array
|
| Return a new array whose items are restricted by typecode, and
| initialized from the optional initializer value, which must be a list,
| string or iterable over elements of the appropriate type.
|
| Arrays represent basic values and behave very much like lists, except
| the type of objects stored in them is constrained. The type is specified
| at object creation time by using a type code, which is a single character.
| The following type codes are defined:
|
| Type code C Type Minimum size in bytes
| 'b' signed integer 1
| 'B' unsigned integer 1
| 'u' Unicode character 2 (see note)
| 'h' signed integer 2
| 'H' unsigned integer 2
| 'i' signed integer 2
| 'I' unsigned integer 2
| 'l' signed integer 4
| 'L' unsigned integer 4
| 'q' signed integer 8 (see note)
| 'Q' unsigned integer 8 (see note)
| 'f' floating point 4
| 'd' floating point 8
|
|
| '__int64'.
|
| Methods:
|
| byteswap() -- byteswap all the items of the array
| fromfile() -- read items from a file object
| fromlist() -- append items from the list
| frombytes() -- append items from the string
| pop() -- remove and return item (default last)
| remove() -- remove first occurrence of an object
| tofile() -- write all items to a file object
| tobytes() -- return the array converted to a string
|
| Attributes:
|
| itemsize -- the length in bytes of one array item
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __copy__(self, /)
| Return a copy of the array.
|
| __deepcopy__(self, unused, /)
| Return a copy of the array.
|
| __delitem__(self, key, /)
| Delete self[key].
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(self, key, /)
| Return self[key].
|
| __gt__(self, value, /)
| Return self>value.
|
| __iadd__(self, value, /)
| Implement self+=value.
|
| __imul__(self, value, /)
| Implement self*=value.
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __mul__(self, value, /)
| Return self*value.n
|
| __ne__(self, value, /)
| Return self!=value.
|
| __new__(*args, **kwargs) from builtins.type
|
| __reduce_ex__(self, value, /)
| Return state information for pickling.
|
| __repr__(self, /)
| Return repr(self).
|
| __rmul__(self, value, /)
| Return self*value.
|
| __setitem__(self, key, value, /)
| Set self[key] to value.
|
| __sizeof__(self, /)
| Size of the array in memory, in bytes.
|
| append(self, v, /)
| Append new value v to the end of the array.
|
| buffer_info(self, /)
|
| the buffer length in bytes.
|
| byteswap(self, /)
| Byteswap all items of the array.
|
| raised.
|
| count(self, v, /)
| Return number of occurrences of v in the array.
|
| extend(self, bb, /)
| Append items to the end of the array.
|
| frombytes(self, buffer, /)
|
| fromfile(self, f, n, /)
|
| fromlist(self, list, /)
| Append items to array from list.
|
| fromstring(self, buffer, /)
|
|
| fromunicode(self, ustr, /)
|
| some other type.
|
| index(self, v, /)
|
| insert(self, i, v, /)
|
| pop(self, i=-1, /)
|
| i defaults to -1.
|
| remove(self, v, /)
| Remove the first occurrence of v in the array.
|
| reverse(self, /)
| Reverse the order of the items in the array.
|
| tobytes(self, /)
|
| tofile(self, f, /)
|
| tolist(self, /)
|
| tostring(self, /)
|
| This method is deprecated. Use tobytes instead.
|
| tounicode(self, /)
|
|
| Data descriptors defined here:
|
| itemsize
| the size, in bytes, of one array item
|
| typecode
| the typecode character used to create the array
|
| Data and other attributes defined here:
|
| __hash__ = None
class array(builtins.object)
| array(typecode [, initializer]) -> array
|
|
| The following type codes are defined:
|
| 'b' signed integer 1
| 'B' unsigned integer 1
| 'u' Unicode character 2 (see note)
| 'h' signed integer 2
| 'H' unsigned integer 2
| 'i' signed integer 2
| 'I' unsigned integer 2
| 'l' signed integer 4
| 'L' unsigned integer 4
| 'q' signed integer 8 (see note)
| 'Q' unsigned integer 8 (see note)
| 'f' floating point 4
| 'd' floating point 8
|
|
| '__int64'.
|
| Methods:
|
| byteswap() -- byteswap all the items of the array
| fromfile() -- read items from a file object
| fromlist() -- append items from the list
| frombytes() -- append items from the string
| pop() -- remove and return item (default last)
| remove() -- remove first occurrence of an object
| tofile() -- write all items to a file object
| tobytes() -- return the array converted to a string
|
| Attributes:
|
| itemsize -- the length in bytes of one array item
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __copy__(self, /)
| Return a copy of the array.
|
| __deepcopy__(self, unused, /)
| Return a copy of the array.
|
| __delitem__(self, key, /)
| Delete self[key].
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(self, key, /)
| Return self[key].
|
| __gt__(self, value, /)
| Return self>value.
|
| __iadd__(self, value, /)
| Implement self+=value.
|
| __imul__(self, value, /)
| Implement self*=value.
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __mul__(self, value, /)
| Return self*value.n
|
| __ne__(self, value, /)
| Return self!=value.
|
| __new__(*args, **kwargs) from builtins.type
|
| __reduce_ex__(self, value, /)
| Return state information for pickling.
|
| __repr__(self, /)
| Return repr(self).
|
| __rmul__(self, value, /)
| Return self*value.
|
| __setitem__(self, key, value, /)
| Set self[key] to value.
|
| __sizeof__(self, /)
| Size of the array in memory, in bytes.
|
| append(self, v, /)
| Append new value v to the end of the array.
|
| buffer_info(self, /)
|
| the buffer length in bytes.
|
| byteswap(self, /)
| Byteswap all items of the array.
|
| raised.
|
| count(self, v, /)
| Return number of occurrences of v in the array.
|
| extend(self, bb, /)
| Append items to the end of the array.
|
| frombytes(self, buffer, /)
|
| fromfile(self, f, n, /)
|
| fromlist(self, list, /)
| Append items to array from list.
|
| fromstring(self, buffer, /)
|
|
| fromunicode(self, ustr, /)
|
| some other type.
|
| index(self, v, /)
|
| insert(self, i, v, /)
|
| pop(self, i=-1, /)
|
| i defaults to -1.
|
| remove(self, v, /)
| Remove the first occurrence of v in the array.
|
| reverse(self, /)
| Reverse the order of the items in the array.
|
| tobytes(self, /)
|
| tofile(self, f, /)
|
| tolist(self, /)
|
| tostring(self, /)
|
| This method is deprecated. Use tobytes instead.
|
| tounicode(self, /)
|
|
| Data descriptors defined here:
|
| itemsize
| the size, in bytes, of one array item
|
| typecode
| the typecode character used to create the array
|
| Data and other attributes defined here:
|
| __hash__ = None
DATA
typecodes = 'bBuhHiIlLqQfd'
FILE
(built-in)
- array基本用法
In [28]: type(s)
Out[28]: array.array
In [29]: s.append(127)
In [30]: s
Out[30]: array('b', [127])
In [31]: s.append(200)
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
<ipython-input-31-a8c01f63058c> in <module>
----> 1 s.append(200)
OverflowError: signed char is greater than maximum
- array排序
In [40]: s
Out[40]: array('b')
In [41]: for i in range(5):
...: s.append(random.randint(1, 10))
...:
In [42]: s
Out[42]: array('b', [6, 6, 3, 5, 5])
In [43]: a = array.array('b', sorted(s))
In [44]: a
Out[44]: array('b', [3, 5, 5, 6, 6])