>>> 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

python之模块array的更多相关文章

  1. 【转】python之模块array

    [转]python之模块array >>> import array#定义了一种序列数据结构 >>> help(array) #创建数组,相当于初始化一个数组,如: ...

  2. python序列化模块json和pickle

    序列化相关 1. json 应用场景: json模块主要用于处理json格式的数据,可以将json格式的数据转化为python的字典,便于python处理,同时也可以将python的字典或列表等对象转 ...

  3. Python常用模块之sys

    Python常用模块之sys sys模块提供了一系列有关Python运行环境的变量和函数. 常见用法 sys.argv 可以用sys.argv获取当前正在执行的命令行参数的参数列表(list). 变量 ...

  4. python 各模块

    01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...

  5. Python常用模块之json模块

    常用模块: 一个Python文件就是一个模块 import xxx 1. 标准模块,Python自带的,如time/random 2. 第三方模块,如连接mysql,需要安装 3. 自己写的Pytho ...

  6. python 序列化模块之 json 和 pickle

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,支持不同程序之间的数据转换.但是只能转换简单的类型如:(列表.字典.字符串. ...

  7. (转)python collections模块详解

    python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...

  8. 5.Python使用模块

    1.模块的 作用 2.模块的含义 3.模块的 导入  因此模块能够划分系统命名空间,避免了不同文件的变量重名的问题.                 Python的模块使得独立的文件连接成了一个巨大 ...

  9. python multiprocessing模块

    python multiprocessing模块 原文地址 multiprocessing multiprocessing支持子进程.通信和共享数据.执行不同形式的同步,提供了Process.Queu ...

随机推荐

  1. 点分治模板(洛谷P4178 Tree)(树分治,树的重心,容斥原理)

    推荐YCB的总结 推荐你谷ysn等巨佬的详细题解 大致流程-- dfs求出当前树的重心 对当前树内经过重心的路径统计答案(一条路径由两条由重心到其它点的子路径合并而成) 容斥减去不合法情况(两条子路径 ...

  2. 51Nod - 1107 斜率小于0的连线数量

    二维平面上N个点之间共有C(n,2)条连线.求这C(n,2)条线中斜率小于0的线的数量. 二维平面上的一个点,根据对应的X Y坐标可以表示为(X,Y).例如:(2,3) (3,4) (1,5) (4, ...

  3. 自学Linux Shell14.3-创建临时文件

    点击返回 自学Linux命令行与Shell脚本之路 14.3-创建临时文件mktemp Linux系统保留了一个特殊的目录位置,以供临时文件使用.Linux使用/tmp目录处理不需要永久保存的文件. ...

  4. mvc4+entityFramework5 发布时遇到的纠结问题

    最近在研究微软的新平台Vs2012,做好的系统在发布到服务器时纠结了.本地环境是win7的,一切运行正常,发布也很顺利.可是悲催的服务器还是windows 2003的,.net framewrok4. ...

  5. JS基本包装类型之三(String)

    1. 基本数据类型和基本包装类型 这里以字符串类型来讲解基本数据类型和基本包装类型. JS中存在基本数据类型String(typeof返回"string"), 也存在基本包装数据类 ...

  6. 通过Cloudera Manager部署CDH5.15.1的webUI界面详解

    通过Cloudera Manager部署CDH5.15.1的webUI界面详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客CDH的部署完全通过Cloudera Mana ...

  7. Spark集群之yarn提交作业优化案例

    Spark集群之yarn提交作业优化案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.启动Hadoop集群 1>.自定义批量管理脚本 [yinzhengjie@s101 ...

  8. 标准遗传算法(二进制编码 python实现)

    代码地址:https://github.com/guojun007/binary_sga 种群初始化: binary_sga/population_init/population_init.py #种 ...

  9. java通过当前请求得到访问者ip的工具类

    在我们开发的过程中,也许有下面的这样的需求,就是要记录一下每次访问服务器的ip,需要存到数据库,以便以后进行数据分析等... 下面给大家介绍一个通过当前请求得到访问者ip的工具类 IpUtil.jav ...

  10. Hive记录-Hive常用命令操作

    1.hive支持四种数据模型 • external table ---外部表:Hive中的外部表和表很类似,但是其数据不是放在自己表所属的目录中,而是存放到别处,这样的好处是如果你要删除这个外部表,该 ...