Python小记——数组(array(一种扁平化序列而非容器序列))

网友投稿 310 2022-08-27


Python小记——数组(array(一种扁平化序列而非容器序列))

如果我们只需要一个包含数字的列表,那么array.array比list更高效。

In [25]: import arrayIn [26]: help(array)Help on built-in module array:NAME arrayDESCRIPTION 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 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

array基本用法

In [27]: s = array.array('b')In [28]: type(s)Out[28]: array.arrayIn [29]: s.append(127)In [30]: sOut[30]: array('b', [127])In [31]: s.append(200)---------------------------------------------------------------------------OverflowError Traceback (most recent call last) in ----> 1 s.append(200)OverflowError: signed char is greater than maximum

array排序

In [39]: import randomIn [40]: sOut[40]: array('b')In [41]: for i in range(5): ...: s.append(random.randint(1, 10)) ...:In [42]: sOut[42]: array('b', [6, 6, 3, 5, 5])In [43]: a = array.array('b', sorted(s))In [44]: aOut[44]: array('b', [3, 5, 5, 6, 6])


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python小记——初始化由列表组成的列表(python列表元素为列表)
下一篇:SpringBoot 错误页面跳转方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~