Python module Array

Source: Internet
Author: User
Tags iterable

>>>Importarray# defines a sequence data structure>>>Help (Array)  
#创建数组, equivalent to initializing an array such as: d={},k=[] and so on array (TypeCode [, initializer])--The create a new array #a =array.array (' C '), determines that the following action is a character and is a single character
#a =array.array (' i '), determines that the following operation is an integer|Attributes:| | TypeCode--The typecode character used to create the array| ItemSize-The lengthinchbytes 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 and I are similar
>>> A.append (8)
>>> A.append (81)
>>> A
Array (' I ', [8, Bayi]) #构成list 

        >>> A=array.array (' C ') #单个字符

>>> a.append (' G ')
>>> a.append (' G ')
>>> A
Array (' C ', ' GG ') #单个字符连接

>>> A=array.array (' u ') #Unicode character, meaning the following is the Unicode string to be entered.
>>> a.append (U ' x ') #不要漏掉u
>>> a.append (U ' x ')
>>> A
Array (' U ', u ' xx ')

|       |   buffer_info (...)     |      Buffer_info ( address, length) #当前内存地址和数组长度
       #返回一个元组 (address, length), gives the current memory address and the length of the buffer used to store the contents of the array

>>> A.buffer_info ()
(19225728, 7)

| Return a tuple (address, length) giving the current memory address and| The lengthinchItems of the buffer used to hold array's Contents|The length should is multiplied by the itemsize attribute to calculate| The buffer lengthinchbytes. | |Byteswap (...) |Byteswap ()|      |  Byteswap all items of the array. If the itemsinchThe array is not1, 2,     | 4,or8 bytesinchSize, RuntimeError israised. | |count (...) |count (x) the number of #统计array数组中某个元素 (x).

>>> A
Array (' I ', [9, 2, 9, 4, 10, 10, 10])
>>> A.count (10)
3
>>> A.count (9)
2

     |      inch The array.     |       |   Extend (...)     |      or iterable) #参数接受 arrays and objects that can be iterated

>>> 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):
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 fromThe file object F andappend them to the end of the|Array.     Also called as read. | |fromlist (...) |fromlist (list)|      | Append items to Array fromlist. | |fromstring (...) |fromstring (String)|      | Appends items fromthe string, interpreting it as an array of machine| Values,asifIt had been read froma file using the FromFile () method). | |Fromunicode (...) |Fromunicode (USTR)|      | Extends this array with data fromThe Unicode string ustr. | The array must be a type'u'array; otherwise a valueerror| israised. Use Array.fromstring (Ustr.decode (...)) to|Append Unicode data to a array of some other type. | |Index (...) |index (x)|      | Return index of first occurrence of XinchThe 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 element, the last element, if the argument is passed by parameter index to delete the element.
3
>>> A
Array (' I ', [2, 9])

| Return the i-th element andDelete it fromThe array. I defaults to-1. | |Read (...) |FromFile (f, N)|      | Read N Objects fromThe file object F andappend them to the end of the|Array.     Also called as read. | |Remove (...) |Remove (x) #删除指定元素, x is the element you want to delete.| Remove the first occurrence of XinchThe array. | |Reverse (...) |reverse ()|      | Reverse the order of the itemsinchThe 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 does not have a list property
>>> a.tolist ()
[9, 2, 9, 4, 10, 10, 10, 3, 5]

     |       Convert Array to a 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数组, converted to a Unicode string string.

>>> A=array.array (' u ')
>>> a.append (U ' Xiaodeng ')

A.append (U ' xiaodeng ')
Typeerror:array item must be Unicode character
>>> A.app End (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  |        |  | ----------------------------------------------------------------------


Type code C type Minimum size in bytes# minimum byte size
' C ' character (character, single 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

Python module Array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.