2012年4月4日 星期三

[Python Std Library] Data Types : array — Efficient arrays of numeric values


翻譯自 這裡
Preface :
此模組提供一個 compact size 的 array, 使用上跟 list 一樣, 差別是 list 可以存放任意物件但 array 只能存放固定型態如整數, 字元, 浮點數等固定型態且固定大小的資料, 而使用上是在建構子提供一個 Type code 說明這個 array 的存放物件是什麼 :


接著來看其建構子 :
- class array.array(typecode[, initializer])
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.

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’s fromlist()fromstring(), or fromunicode() method (see below) to add initial items to the array. Otherwise, the iterable initializer is passed to the extend() method.

在使用上與 list 大同小異 (indexing, slicing, concatenation, and multiplication), 唯一要注意的是如果你使用與原先 array 定義不同的物件進行存入會引發 TypeError 例外. 下面是物件上提供的函數與屬性 :
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.

array.buffer_info()
Return a tuple (address, length) giving the current memory address and the length in elements of the buffer used to hold array’s contents. The size of the memory buffer in bytes can be computed as array.buffer_info()[1] * array.itemsize. This is occasionally useful when working with low-level (and inherently unsafe) I/O interfaces that require memory addresses, such as certain ioctl() operations. The returned numbers are valid as long as the array exists and no length-changing operations are applied to it.

array.byteswap()
“Byteswap” all items of the array. This is only supported for values which are 1, 2, 4, or 8 bytes in size; for other types of values, RuntimeError is raised. It is useful when reading data from a file written on a machine with a different byte order.

array.count(x)
Return the number of occurrences of x in the array.

array.extend(iterable)
Append items from iterable to the end of the array. If iterable is another array, it must have exactly the same type code; if not, TypeError will be raised. If iterable is not an array, it must be iterable and its elements must be the right type to be appended to the array.

Changed in version 2.4: Formerly, the argument could only be another array.

array.fromfile(fn)
Read n items (as machine values) from the file object f and append them to the end of the array. If less than n items are available, EOFError is raised, but the items that were available are still inserted into the array. f must be a real built-in file object; something else with a read() method won’t do.

array.fromlist(list)
Append items from the list. This is equivalent to for x in list: a.append(x) except that if there is a type error, the array is unchanged.

array.fromstring(s)
Appends items from the string, interpreting the string as an array of machine values (as if it had been read from a file using the fromfile() method).

array.fromunicode(s)
Extends this array with data from the given unicode string. The array must be a type 'u' array; otherwise a ValueError is raised. Usearray.fromstring(unicodestring.encode(enc)) to append Unicode data to an array of some other type.

array.index(x)
Return the smallest i such that i is the index of the first occurrence of x in the array.

array.insert(ix)
Insert a new item with value x in the array before position i. Negative values are treated as being relative to the end of the array.
>>> arr = array('l')
>>> arr.extend([1, 3, 4, 5])
>>> arr.insert(1, 2)
>>> arr
array('l', [1, 2, 3, 4, 5])

array.pop() 
Removes the item with the index [i]i from the array and returns it. The optional argument defaults to -1, so that by default the last item is removed and returned.

array.remove(x)
Remove the first occurrence of x from the array.

array.reverse()
Reverse the order of the items in the array.

array.tofile(f)
Write all items (as machine values) to the file object f.

array.tolist()
Convert the array to an ordinary list with the same items.

array.tostring()
Convert the array to an array of machine values and return the string representation (the same sequence of bytes that would be written to a file by the tofile()method.)

array.tounicode()
Convert the array to a unicode string. The array must be a type 'u' array; otherwise a ValueError is raised. Use array.tostring().decode(enc) to obtain a unicode string from an array of some other type.
This message was edited 13 times. Last update was at 03/04/2012 10:37:54

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...