python内置array模块

这个模块定义了一个对象类型,用以表示一些基础变量构成的列表,包括字符,整数,浮点数,Array是序列类型,使用起来与list十分接近,但是存储的变量类型只能是一种,所以方便高效的数值运算。可以使用type code在创建array时指定内部变量的类型,type code定义如下表:

Type codeC TypePython TypeMinimum size in bytesNotes
‘b’signed charint1
‘B’unsigned charint1
‘u’Py_UNICODEUnicode character2(1)
‘h’signed shortint2
‘H’unsigned shortint2
‘i’signed intint2
‘I’unsigned intint2
‘l’signed longint4
‘L’unsigned longint4
‘q’signed long longint8(2)
‘Q’unsigned long longint8(2)
‘f’floatfloat4
‘d’doublefloat8

注意:
1.u类型长度与平台有关,有可能16bits或32bits,此类型将会在python4.0中被移除
2.q或Q类型只能在C编译器支持long long类型中使用

此模块定义了如下类型:
class array.array(typecode[, initializer])

typecode限制了array的类型,initializer为可选项,终于初始化,这个参数必须是一个列表,一个类似于字节的对象,或者对适当类型的元素进行迭代的迭代器。
如果给定一个列表或字符串,那么初始化器就会被传递给新的数组的fromlist()、frombytes()或fromunicode()方法(见下面),并将初始项添加到数组中。否则,迭代初始化器将被传递给扩展()方法

 

Python
>>> import array#定义了一种序列数据结构 >>> help(array)     #创建数组,相当于初始化一个数组,如:d={},k=[]等等 array(typecode [, initializer]) -- create a new array   #a=array.array('c'),决定着下面操作的是字符,并是单个字符   #a=array.array('i'),决定着下面操作的是整数 | Attributes: | | typecode -- the typecode character used to create the array | itemsize -- the length in bytes of one array item | | Methods defined here: | •append(...) | append(x) | #向array数组添加一个数值value | Append new value x to the end of the array.         >>> a=array.array('i')#整数,b与i类似         >>> a.append(8)         >>> a.append(81)         >>> a         array('i', [8, 81])#构成list          >>> a=array.array('c')#单个字符         >>> a.append('g')         >>> a.append('g')         >>> a         array('c', 'gg')#单个字符连接         >>> a=array.array('u')#Unicode character,意味着下面将要输入的是unicode字符串.         >>> a.append(u'x')#不要漏掉u         >>> a.append(u'x')         >>> a         array('u', u'xx') | | •buffer_info(...) | buffer_info() -> (address, length)#当前内存地址和数组长度        #返回一个元组(地址,长度),给出了当前的内存地址和用于存储数组内容的缓冲区的长度         >>> a.buffer_info()         (19225728, 7) | Return a tuple (address, length) giving the current memory address and | the length in items of the buffer used to hold array's contents | The length should be multiplied by the itemsize attribute to calculate | the buffer length in bytes. | | byteswap(...) | byteswap() | | Byteswap all items of the array. If the items in the array are not 1, 2, | 4, or 8 bytes in size, RuntimeError is raised. | | •count(...) | count(x)    #统计array数组中某个元素(x)的个数.         >>> a         array('i', [9, 2, 9, 4, 10, 10, 10])         >>> a.count(10)         >>> a.count(9) | Return number of occurrences of x in the array. | | •extend(...) | extend(array or iterable)  #参数接受 数组和可迭代对象         >>> a         array('i', [9, 2, 9, 4, 10, 10, 10])         >>> a.extend([3,5])         >>> a         array('i', [9, 2, 9, 4, 10, 10, 10, 3, 5])         #如果添加整数会出现什么错误?         >>> a.extend(10)         Traceback (most recent call last):         File "<pyshell#131>", line 1, in <module>         a.extend(10)         TypeError: 'int' object is not iterable #int不是可迭代对象 | Append items to the end of the array.#在末尾添加数组或可迭代对象 | | fromfile(...) | fromfile(f, n) | | Read n objects from the file object f and append them to the end of the | array. Also called as read. | | fromlist(...) | fromlist(list) | | Append items to array from list. | | fromstring(...) | fromstring(string) | | Appends items from the string, interpreting it as an array of machine | values,as if it had been read from a file using the fromfile() method). | | fromunicode(...) | fromunicode(ustr) | | Extends this array with data from the unicode string ustr. | The array must be a type 'u' array; otherwise a ValueError | is raised. Use array.fromstring(ustr.decode(...)) to | append Unicode data to an array of some other type. | | index(...) | index(x) | | Return index of first occurrence of x in the array. | | •insert(...) | insert(i,x)  #在i的位置插入一个新的item在array中 | | Insert a new item x into the array before position i. | | •pop(...) | pop([i])         >>> a=array.array('i')         >>> a.append(2)         >>> a.append(9)         >>> a.append(3)         >>> a         array('i', [2, 9, 3])         >>> a.pop()#默认删除索引为-1的元素,最后一个元素,如果传参数则按参数索引来删除元素.         >>> a         array('i', [2, 9]) | Return the i-th element and delete it from the array. i defaults to -1. | | read(...) | fromfile(f, n) | | Read n objects from the file object f and append them to the end of the | array. Also called as read. | | •remove(...) | remove(x)#删除指定元素,x为需要删除的元素. | Remove the first occurrence of x in the array. | | reverse(...) | reverse() | | Reverse the order of the items in the array. | | tofile(...) | tofile(f) | | Write all items (as machine values) to the file object f. Also called as | write. | | •tolist(...) | tolist() -> list         a=array('i', [9, 2, 9, 4, 10, 10, 10, 3, 5])         >>> a.list()         a.list()         AttributeError: 'array.array' object has no attribute 'list'#array.array没有list属性         >>> a.tolist()         [9, 2, 9, 4, 10, 10, 10, 3, 5] | Convert array to an ordinary list with the same items. | | •tostring(...) | tostring() -> string         array('i', [9, 2, 9, 4])         >>> a.tostring()    #转化为string         '\t\x00\x00\x00\x02\x00\x00\x00\t\x00\x00\x00\x04\x00\x00\x00' | •tounicode(...) | tounicode() -> unicode #将unicode的array数组,转化为unicode string字符串。         >>> a=array.array('u')         >>> a.append(u'xiaodeng')         a.append(u'xiaodeng')         TypeError: array item must be unicode character         >>> a.append(u'x')         >>> a.append(u'i')         >>> a.tounicode()         u'xi' | | write(...) | tofile(f) | | Write all items (as machine values) to the file object f. Also called as | write. | | ---------------------------------------------------------------------- | Data descriptors defined here: | | itemsize | the size, in bytes, of one array item | | typecode | the typecode character used to create the array | | ---------------------------------------------------------------------- Type code   C Type         Minimum size in bytes#最小字节大小 'c'      character (字符,单个字符)       1 'b'      signed integer     1 'B'      unsigned integer   1 'u'      Unicode character   2 'h'      signed integer     2 'H'     unsigned integer    2 'i'     signed integer     2 'I'      unsigned integer    2 'l'      signed integer     4 'L'      unsigned integer   4 'f'      floating point     4 'd'      floating point     8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
>>> import array #定义了一种序列数据结构
>>> help ( array )  
   #创建数组,相当于初始化一个数组,如:d={},k=[]等等
     array ( typecode [ , initializer ] ) -- create a new array
   #a=array.array('c'),决定着下面操作的是字符,并是单个字符
   #a=array.array('i'),决定着下面操作的是整数
     |    Attributes :
     |   
     |    typecode -- the typecode character used to create the array
     |    itemsize -- the length in bytes of one array item
     |   
     |    Methods defined here :
     |   • append ( . . . )
     |        append ( x )
     |        #向array数组添加一个数值value
     |        Append new value x to the end of the array .
 
         >>> a = array . array ( 'i' ) #整数,b与i类似
         >>> a . append ( 8 )
         >>> a . append ( 81 )
         >>> a
         array ( 'i' , [ 8 , 81 ] ) #构成list 
         >>> a = array . array ( 'c' ) #单个字符
         >>> a . append ( 'g' )
         >>> a . append ( 'g' )
         >>> a
         array ( 'c' , 'gg' ) #单个字符连接
         >>> a = array . array ( 'u' ) #Unicode character,意味着下面将要输入的是unicode字符串.
         >>> a . append ( u 'x' ) #不要漏掉u
         >>> a . append ( u 'x' )
         >>> a
         array ( 'u' , u 'xx' )
|   
     |   • buffer_info ( . . . )
     |        buffer_info ( ) -> ( address , length ) #当前内存地址和数组长度
          #返回一个元组(地址,长度),给出了当前的内存地址和用于存储数组内容的缓冲区的长度
         >>> a . buffer_info ( )
         ( 19225728 , 7 )
     |        Return a tuple ( address , length ) giving the current memory address and
     |        the length in items of the buffer used to hold array 's contents
     |      The length should be multiplied by the itemsize attribute to calculate
     |      the buffer length in bytes.
     |  
     |  byteswap(...)
     |      byteswap()
     |      
     |      Byteswap all items of the array.  If the items in the array are not 1, 2,
     |      4, or 8 bytes in size, RuntimeError is raised.
     |  
     |  •count(...)
     |      count(x)    #统计array数组中某个元素(x)的个数.
 
        >>> a
        array(' i ', [9, 2, 9, 4, 10, 10, 10])
        >>> a.count(10)
        >>> a.count(9)
     |      Return number of occurrences of x in the array.
     |  
     |  •extend(...)
     |      extend(array or iterable)  #参数接受 数组和可迭代对象
 
        >>> a
        array(' i ', [9, 2, 9, 4, 10, 10, 10])
        >>> a.extend([3,5])
        >>> a
        array(' i ', [9, 2, 9, 4, 10, 10, 10, 3, 5])
        #如果添加整数会出现什么错误?
        >>> a.extend(10)
        Traceback (most recent call last):
          File "<pyshell#131>", line 1, in <module>
            a.extend(10)
        TypeError: ' int ' object is not iterable  #int不是可迭代对象
     |      Append items to the end of the array.#在末尾添加数组或可迭代对象
     |  
     |  fromfile(...)
     |      fromfile(f, n)
     |      
     |      Read n objects from the file object f and append them to the end of the
     |      array.  Also called as read.
     |  
     |  fromlist(...)
     |      fromlist(list)
     |      
     |      Append items to array from list.
     |  
     |  fromstring(...)
     |      fromstring(string)
     |      
     |      Appends items from the string, interpreting it as an array of machine
     |      values,as if it had been read from a file using the fromfile() method).
     |  
     |  fromunicode(...)
     |      fromunicode(ustr)
     |      
     |      Extends this array with data from the unicode string ustr.
     |      The array must be a type ' u ' array; otherwise a ValueError
     |      is raised.  Use array.fromstring(ustr.decode(...)) to
     |      append Unicode data to an array of some other type.
     |  
     |  index(...)
     |      index(x)
     |      
     |      Return index of first occurrence of x in the array.
     |  
     |  •insert(...)
     |      insert(i,x)  #在i的位置插入一个新的item在array中
     |      
     |      Insert a new item x into the array before position i.
     |  
     |  •pop(...)
     |      pop([i])
 
        >>> a=array.array(' i ')
        >>> a.append(2)
        >>> a.append(9)
        >>> a.append(3)
        >>> a
        array(' i ', [2, 9, 3])
        >>> a.pop()#默认删除索引为-1的元素,最后一个元素,如果传参数则按参数索引来删除元素.
        >>> a
        array(' i ', [2, 9])
     |      Return the i-th element and delete it from the array. i defaults to -1.
     |  
     |  read(...)
     |      fromfile(f, n)
     |      
     |      Read n objects from the file object f and append them to the end of the
     |      array.  Also called as read.
     |  
     |  •remove(...)
     |      remove(x)#删除指定元素,x为需要删除的元素.
     |      Remove the first occurrence of x in the array.
     |  
     |  reverse(...)
     |      reverse()
     |      
     |      Reverse the order of the items in the array.
     |  
     |  tofile(...)
     |      tofile(f)
     |      
     |      Write all items (as machine values) to the file object f.  Also called as
     |      write.
     |  
     |  •tolist(...)
     |      tolist() -> list
        a=array(' i ', [9, 2, 9, 4, 10, 10, 10, 3, 5])
        >>> a.list()
        a.list()
        AttributeError: ' array . array ' object has no attribute ' list '#array.array没有list属性
        >>> a.tolist()
        [9, 2, 9, 4, 10, 10, 10, 3, 5]
     |      Convert array to an ordinary list with the same items.
     |  
     |  •tostring(...)
     |      tostring() -> string
        array(' i ', [9, 2, 9, 4])
        >>> a.tostring()    #转化为string
        ' \ t \ x00 \ x00 \ x00 \ x02 \ x00 \ x00 \ x00 \ t \ x00 \ x00 \ x00 \ x04 \ x00 \ x00 \ x00 '
     |  •tounicode(...)
     |      tounicode() -> unicode  #将unicode的array数组,转化为unicode string字符串。
 
        >>> a=array.array(' u ')
        >>> a.append(u' xiaodeng ')
            a.append(u' xiaodeng ')
        TypeError: array item must be unicode character
        >>> a.append(u' x ')
        >>> a.append(u' i ')
        >>> a.tounicode()
        u' xi '
|  
     |  write(...)
     |      tofile(f)
     |      
     |      Write all items (as machine values) to the file object f.  Also called as
     |      write.
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |  
     |  itemsize
     |      the size, in bytes, of one array item
     |  
     |  typecode
     |      the typecode character used to create the array
     |  
     |  ----------------------------------------------------------------------
 
 
 
Type code   C Type         Minimum size in bytes#最小字节大小
        ' c '      character (字符,单个字符)               1
        ' b '      signed integer     1
        ' B '             unsigned integer      1
        ' u '             Unicode character    2
        ' h '             signed integer     2
        ' H '      unsigned integer     2
        ' i '      signed integer     2
        ' I '             unsigned integer     2
        ' l '             signed integer     4
        ' L '             unsigned integer    4
        ' f '             floating point     4
        ' d'               floating point      8

 

 




  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值