python标准库-array 模块
原文地址:http://www.bugingcode.com/blog/python_module_array.html
array 模块是python中实现的一种高效的数组存储类型。它和list相似,但是所有的数组成员必须是同一种类型,在创建数组的时候,就确定了数组的类型。
| Type code | C Type | Python Type | Minimum size in bytes |
|---|---|---|---|
| 'c' | char | character | 1 |
| 'b' | signed char | int | 1 |
| 'B' | unsigned char | int | 1 |
| 'u' | Py_UNICODE | Unicode character | 2 (see note) |
| 'h' | signed short | int | 2 |
| 'H' | unsigned short | int | 2 |
| 'i' | signed int | int | 2 |
| 'I' | unsigned int | long | 2 |
| 'l' | signed long | int | 4 |
| 'L' | unsigned long | long | 4 |
| 'f' | float | float | 4 |
| 'd' | double | float | 8 |
创建 一组 int的数组:
#!/usr/bin/python
#coding=utf-8
import array
a = array.array("i", range(16))
for i in a:
print i
下面是一组关于array的例子,是通过tostring 方法把内部结构转为一个字符串。
例子:使用array模块将一个int的数组转换为字符串。
import array
a = array.array("B", range(16)) # unsigned char
b = array.array("h", range(16)) # signed short
print a
print repr(a.tostring())
print b
print repr(b.tostring())
输出如下:
array('B', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
'\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017'
array('h', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
'\000\000\001\000\002\000\003\000\004\000\005\000\006\000\007\000
\010\000\011\000\012\000\013\000\014\000\015\000\016\000\017\000'
在某种程度上,**array ** 可以被当成一个普通的序列,但是这个序列里不能出现不同类型的变量。
例子:把arrays 当成是普通序列
import array
a = array.array("B", [1, 2, 3])
a.append(4)
a = a + a
a = a[2:-2]
print a
print repr(a.tostring())
for i in a:
print i,
结果如下:
array('B', [3, 4, 1, 2])
'\003\004\001\002'
3 4 1 2
**array **模块同时提供了一个高效的方法把原始二进制转换为一个整数系列。
例子:把字符串转换为整数数组
import array
a = array.array("i", "fish license") # signed integer
print a
print repr(a.tostring())
print a.tolist()
结果如下:
array('i', [1752394086, 1667853344, 1702063717])
'fish license'
[1752394086, 1667853344, 1702063717]
最后看看array模块是如何确定当前平台的字节存储顺序。
例子:使用array 模块确定当前平台的字节存储顺序
import array
def little_endian():
return ord(array.array("i",[1]).tostring()[0])
if little_endian():
print "little-endian platform (intel, alpha)"
else:
print "big-endian platform (motorola, sparc)"
结果如下:
big-endian platform (motorola, sparc)
python 2.0 以上也可以使用 sys.byteorder,它返回结果为“little” 或者“big”:
例子:使用sys.byteorder模块确定当前平台的字节存储顺序
import sys
# available in Python 2.0 and later
if sys.byteorder == "little":
print "little-endian platform (intel, alpha)"
else:
print "big-endian platform (motorola, sparc)"
输出如下:
'big-endian platform (motorola, sparc)'
转载请标明来之:http://www.bugingcode.com/
更多教程:阿猫学编程
python标准库-array 模块的更多相关文章
- [python标准库]Pickle模块
Pickle-------python对象序列化 本文主要阐述以下几点: 1.pickle模块简介 2.pickle模块提供的方法 3.注意事项 4.实例解析 1.pickle模块简介 The pic ...
- Python 标准库 ConfigParser 模块 的使用
Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import ...
- Python标准库——collections模块的Counter类
1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...
- [python标准库]XML模块
1.什么是XML XML是可扩展标记语言(Extensible Markup Language)的缩写,其中的 标记(markup)是关键部分.您可以创建内容,然后使用限定标记标记它,从而使每个单词. ...
- 【python】Python标准库defaultdict模块
来源:http://www.ynpxrz.com/n1031711c2023.aspx Python标准库中collections对集合类型的数据结构进行了很多拓展操作,这些操作在我们使用集合的时候会 ...
- Python标准库--os模块
这个模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Windows下运行.一个例 ...
- python标准库 bisect模块
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #bisect #作用:维护有序列表,而不必在每次向列表增加一个元素 ...
- python标准库 sysconfig模块
# -*- coding: utf-8 -*-# python:2.x__author__ = 'Administrator'import sysconfig#sysconfig:解释器编译时配置#作 ...
- Python标准库 -- UUID模块(生成唯一标识)
UUID是什么: UUID: 通用唯一标识符 ( Universally Unique Identifier ),对于所有的UUID它可以保证在空间和时间上的唯一性,也称为GUID,全称为: UUID ...
随机推荐
- Python 中 JSON和dict的转换,json的使用
一. 基础语法 在Python 的 json库中,共有四个方法.分别是: json.load() # 从文件中加载 json.loads() # 数据中加载 json.dump() # 转存到文件 j ...
- c指针(1)
#include<stdio.h> void swap(int *a,int *b); void dummy_swap(int *a,int *b); int main() { ,d=; ...
- scp 碰到的问题
将 nodejs 的全局目录scp复制到另外一台机器部署代码, 发现运行报错, 提示缺少依赖模块. 检查了很久, 没发现问题. 后来发现,软链接 scp后不再是软链接而是对应文件, 导致相对路径改变!
- 关于mysql数据库连接异常处理
tomcat启动错误日志关键信息: 28-Aug-2019 14:22:55.014 SEVERE [localhost-startStop-1] org.apache.catalina.core.C ...
- linux(centos 7)安装及使用yum
yum介绍: Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的 ...
- PANIC: ANDROID_SDK_HOME is defined but could not find Nexus_5_API_23.ini file in $ANDROID_SDK_HOME\
运行模拟器总是出现这个错误 后来把系统环境变量中的ANDROID_SDK_HOME 删掉就好了 我去,好神奇的操作
- Codeforces 1293A - ConneR and the A.R.C. Markland-N
题目大意: ConneR老师想吃东西,他现在在大楼的第s层,大楼总共有n层,但是其中有k层的餐厅关门了. 然后给了这k层关门的餐厅分别所在的楼层. 所以问ConneR老师最少得往上(或者往下)走几层楼 ...
- [数学][欧拉降幂定理]Exponial
Exponial 题目 http://exam.upc.edu.cn/problem.php?cid=1512&pid=4 欧拉降幂定理:当b>phi(p)时,有a^b%p = a^(b ...
- Python程序在docker中运行,未找到自定义模块
错误 Traceback (most recent call last): File "demo.py", line 13, in <module> from test ...
- 【shell】概述
功能简介 批量自动初始化系统(update,软件安装,时区设置,安全策略...) 批量自动部署软件(LAMP,LNMP,Nginx,LVS,Tomcat) 管理应用程序(KVM,集群管理扩容,MySQ ...