>>> 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)
3
>>> a.count(9)
2
| 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的元素,最后一個元素,如果傳參數則按參數索引來刪除元素.
3
>>> 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
