Python学习笔记之array

Array

This module defines an object type which can compactly represent an array ofbasic values: characters, integers, floating point numbers. Arrays are sequencetypes and behave very much like lists, except that the type of objects stored inthem is constrained. The type is specified at object creation time by using atype code, which is a single character. The following type codes aredefined:

Type codeC TypePython TypeMinimum size in bytes
'c'charcharacter1
'b'signed charint1
'B'unsigned charint1
'u'Py_UNICODEUnicode character2 (see note)
'h'signed shortint2
'H'unsigned shortint2
'i'signed intint2
'I'unsigned intlong2
'l'signed longint4
'L'unsigned longlong4
'f'floatfloat4
'd'doublefloat8

Note

The 'u' typecode corresponds to Python’s unicode character. On narrowUnicode builds this is 2-bytes, on wide builds this is 4-bytes.

The actual representation of values is determined by the machine architecture(strictly speaking, by the C implementation). The actual size can be accessedthrough the itemsize attribute. The values stored for 'L' and'I' items will be represented as Python long integers when retrieved,because Python’s plain integer type cannot represent the full range of C’sunsigned (long) integers.

The module defines the following type:

class array. array ( typecode [, initializer ] )

A new array whose items are restricted by typecode, and initializedfrom the optional initializer value, which must be a list, string, or iterableover elements of the appropriate type.

Changed in version 2.4: Formerly, only lists or strings were accepted.

If given a list or string, the initializer is passed to the new array’sfromlist(), fromstring(), or fromunicode() method (see below)to add initial items to the array. Otherwise, the iterable initializer ispassed to the extend() method.

array. ArrayType

Obsolete alias for array.

Array objects support the ordinary sequence operations of indexing, slicing,concatenation, and multiplication. When using slice assignment, the assignedvalue must be an array object with the same type code; in all other cases,TypeError is raised. Array objects also implement the buffer interface,and may be used wherever buffer objects are supported.

The following data items and methods are also supported:

array. typecode

The typecode character used to create the array.

array. itemsize

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

array. append ( x )

Append a new item with value x to the end of the array.

Note:在Python3.6下有些变化,有些typecode约束不支持一些初始化器,如下图所示:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值