博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python中的数组
阅读量:4052 次
发布时间:2019-05-25

本文共 1076 字,大约阅读时间需要 3 分钟。

1 数组的创建:

涉及的module: array

 

An array object is similar to a list except that it can hold only certain types of simple data and only one type at any given time. when you create an array object,you specify which type of data it will hold:

 

>>> import array

>>> z=array.array('B')
>>> z.append(5)
>>> z
array('B', [5])
>>> z[0] 
5
>>> q= array.array('i',[5,10,20])
>>> q
array('i', [5, 10, 20])
>>> q[0] 
5

 

数组支持的数据类型:

 

求数组的长度:

arrayname.itemsize;

>>> q.itemsize

4

 

数组和list 之间转换:

数组转换为list:toList(): converts the array to an ordinary list

>>> q.tolist()

[5, 10, 20]

 

将一个list添加到数组的末尾:

>>> q

array('i', [5, 10, 20])
>>> q.fromlist([3,4])
>>> q
array('i', [5, 10, 20, 3, 4])

 

array 和string之间的相互转换:

tostring():onvert an array to a sequence of bytes using the tostring() methond.

fromstring():taking a string of bytes and converting them to values for the array

 

数组和文件之间的互操作:

tofile(file):convert the array to a sequence of bytes and writes the resulting bytes to a file you pass in

fromfiles(file,count): reads the specifiled number of items in from a file object and appends them to the array.

 

转载地址:http://anxci.baihongyu.com/

你可能感兴趣的文章
数据库索引介绍及使用
查看>>
MongoDB数据库插入、更新和删除操作详解
查看>>
MongoDB文档(Document)全局唯一ID的设计思路
查看>>
mongoDB简介
查看>>
Redis持久化存储(AOF与RDB两种模式)
查看>>
memcached工作原理与优化建议
查看>>
Redis与Memcached的区别
查看>>
redis sharding方案
查看>>
程序员最核心的竞争力是什么?
查看>>
Node.js机制及原理理解初步
查看>>
linux CPU个数查看
查看>>
分布式应用开发相关的面试题收集
查看>>
简单理解Socket及TCP/IP、Http、Socket的区别
查看>>
利用HTTP Cache来优化网站
查看>>
利用负载均衡优化和加速HTTP应用
查看>>
消息队列设计精要
查看>>
分布式缓存负载均衡负载均衡的缓存处理:虚拟节点对一致性hash的改进
查看>>
分布式存储系统设计(1)—— 系统架构
查看>>
MySQL数据库的高可用方案总结
查看>>
常用排序算法总结(一) 比较算法总结
查看>>