python的array初识
from array import array
"""
’b’
signed char
int
1
’B’
unsigned char
int
1
’u’
Py_UNICODE
Unicode character
2
(1)
’h’
signed short
int
2
’H’
unsigned short
int
2
’i’
signed int
int
2
’I’
unsigned int
int
2
’l’
signed long
int
4
’L’
unsigned long
int
4
’q’
signed long long
int
8
(2)
’Q’
unsigned long long
int
8
(2)
’f’
float
float
4
’d’
double
float
8
"""
buffer = array('l') # 创建有符号类型到整数数组 L是无符号类型的整型,无符号表示正整数包括0,有符号是正负整数和0
buffer.append(127)
buffer.append(10) # 追加元素到末尾
print(buffer.count(10))
buffer.extend([4, 2]) # 扩展数组
buffer.fromlist([21, 4, 12, -128, 0]) # 从列表添加元素,与extend效果一样
print(buffer)
buffer.pop() # 移除最后一个元素
print(buffer)
print('buffer_info->', buffer.buffer_info())
# print(buffer.byteswap())
print('itemsize->', buffer.itemsize)
print('tostring->', buffer.tostring())
print('tobytes->', buffer.tobytes())
print('tolist->', buffer.tolist())
f = open('test.txt', 'wb')
print('tofile->',buffer.tofile(f))
print('typecode->', buffer.typecode)
#
#
buffer = array('f') # 创建浮点类型数组,对于整数会自动转为浮点数
buffer.append(127)
buffer.append(10) # 追加元素到末尾
print(buffer.count(10))
buffer.extend([4, 2]) # 扩展数组
buffer.fromlist([21, 4, 12, -128, 0]) # 从列表添加元素,与extend效果一样
print(buffer)
buffer.pop() # 移除最后一个元素
print(buffer)
print('buffer_info->', buffer.buffer_info())
# print(buffer.byteswap())
print('itemsize->', buffer.itemsize)
print('tostring->', buffer.tostring())
print('tobytes->', buffer.tobytes())
print('tolist->', buffer.tolist())
f = open('test.txt', 'wb')
print('tofile->',buffer.tofile(f))
print('typecode->', buffer.typecode)
buffer = array('b') # 创建字符型的的整型数组,该元素范围为-128~127之间,对应的是
buffer.append(127)
buffer.append(10) # 追加元素到末尾
print(buffer.count(10))
buffer.extend([4, 2]) # 扩展数组
buffer.fromlist([21, 4, 12, -128, 0]) # 从列表添加元素,与extend效果一样
buffer.frombytes(b'hello world')
print(buffer)
buffer.pop() # 移除最后一个元素
print(buffer)
print('buffer_info->', buffer.buffer_info())
# print(buffer.byteswap())
print('itemsize->', buffer.itemsize)
print('tostring->', buffer.tostring())
print('tobytes->', buffer.tobytes())
print('tolist->', buffer.tolist())
f = open('test.txt', 'wb')
print('tofile->',buffer.tofile(f))
print('typecode->', buffer.typecode)
buffer = array('B') # 创建字符型的的整型数组,该元素范围为0~255之间,对应的是ascii表的十进制数
buffer.append(127)
buffer.append(10) # 追加元素到末尾
print(buffer.count(10))
buffer.extend([4, 2]) # 扩展数组
buffer.fromlist([21, 4, 12, 255, 0]) # 从列表添加元素,与extend效果一样
buffer.frombytes(b'hello worlded')
# buffer.append(100)
print(buffer)
buffer.pop() # 移除最后一个元素
print(buffer)
print('buffer_info->', buffer.buffer_info())
# print(buffer.byteswap())
print('itemsize->', buffer.itemsize)
print('tostring->', buffer.tostring().decode('Latin-1')) # 对于含有特殊指令符的数据只能用这种编码解码,普通的字符可以使用默认的utf-8解码
print('tobytes->', buffer.tobytes())
print('tolist->', buffer.tolist())
f = open('test.txt', 'wb')
print('tofile->',buffer.tofile(f)) # 写入文件
print('typecode->', buffer.typecode)
"""
1
array('l', [127, 10, 4, 2, 21, 4, 12, -128, 0])
array('l', [127, 10, 4, 2, 21, 4, 12, -128])
buffer_info-> (4334510800, 8)
itemsize-> 8
tostring-> b'\x7f\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x15\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x80\xff\xff\xff\xff\xff\xff\xff'
tobytes-> b'\x7f\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x15\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x80\xff\xff\xff\xff\xff\xff\xff'
tolist-> [127, 10, 4, 2, 21, 4, 12, -128]
tofile-> None
typecode-> l
1
array('f', [127.0, 10.0, 4.0, 2.0, 21.0, 4.0, 12.0, -128.0, 0.0])
array('f', [127.0, 10.0, 4.0, 2.0, 21.0, 4.0, 12.0, -128.0])
buffer_info-> (4334315536, 8)
itemsize-> 4
tostring-> b'\x00\x00\xfeB\x00\x00 A\x00\x00\x80@\x00\x00\x00@\x00\x00\xa8A\x00\x00\x80@\x00\x00@A\x00\x00\x00\xc3'
tobytes-> b'\x00\x00\xfeB\x00\x00 A\x00\x00\x80@\x00\x00\x00@\x00\x00\xa8A\x00\x00\x80@\x00\x00@A\x00\x00\x00\xc3'
tolist-> [127.0, 10.0, 4.0, 2.0, 21.0, 4.0, 12.0, -128.0]
tofile-> None
typecode-> f
1
array('b', [127, 10, 4, 2, 21, 4, 12, -128, 0, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100])
array('b', [127, 10, 4, 2, 21, 4, 12, -128, 0, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108])
buffer_info-> (4334041264, 19)
itemsize-> 1
tostring-> b'\x7f\n\x04\x02\x15\x04\x0c\x80\x00hello worl'
tobytes-> b'\x7f\n\x04\x02\x15\x04\x0c\x80\x00hello worl'
tolist-> [127, 10, 4, 2, 21, 4, 12, -128, 0, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108]
tofile-> None
typecode-> b
1
array('B', [127, 10, 4, 2, 21, 4, 12, 255, 0, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 101, 100])
array('B', [127, 10, 4, 2, 21, 4, 12, 255, 0, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 101])
buffer_info-> (4334041264, 21)
itemsize-> 1
tostring->
ÿ�hello worlde
tobytes-> b'\x7f\n\x04\x02\x15\x04\x0c\xff\x00hello worlde'
tolist-> [127, 10, 4, 2, 21, 4, 12, 255, 0, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 101]
tofile-> None
typecode-> B
"""
python的array初识的更多相关文章
- codefroce D. Powerful array[初识块状数组]
codefroce D. Powerful array[初识块状数组] 由于是初始所以,仅仅能先用别人的分析.囧... 题目: 给定一个数列:A1, A2,--,An,定义Ks为区间(l,r)中s出现 ...
- 孤荷凌寒自学python第二十一天初识python的类
孤荷凌寒自学python第二十一天初识python的类 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 类是面向对象的编程语言非常重要的概念. 编程语言的进化史中从顺序编程到结构化编程,最后才 ...
- python numpy array 与matrix 乘方
python numpy array 与matrix 乘方 编程语言 waitig 1年前 (2017-04-18) 1272℃ 百度已收录 0评论 数组array 的乘方(**为乘方运算符)是每个元 ...
- Python 入门 之 初识面向对象
Python 入门 之 初识面向对象 1.初识面向对象编程 (核心--对象) (1)观察以下代码: # 面向过程编程 s = "alexdsb" count = 0 for i i ...
- python函数-函数初识
python函数-函数初识 1.函数的定义 语法 def 函数名(参数1,参数2,参数3,...): '''注释''' 函数体 return 返回的值 2.函数的使用原则---先定义后调用 #定义阶段 ...
- 01----python入门----python安装与初识
----python入门----python安装与初识 python是一门编程语言,作为学习python的开始,需要事先搞明白:编程的目的是什么?什么是编程语言?什么是编程? 这是一个很好的开头!但是 ...
- Python之路【第五篇】python基础 之初识函数(一)和文件管理
转载请注明出处http://www.cnblogs.com/wupeiqi/articles/5453708.html 函数 一.背景 ...
- python numpy array 的一些问题
1 将list转换成array 如果list的嵌套数组是不规整的,如 a = [[1,2], [3,4,5]] 则a = numpy.array(a)之后 a的type是ndarray,但是a中得元素 ...
- python之路——初识函数
阅读目录 为什么要用函数 函数的定义与调用 函数的返回值 函数的参数 本章小结 返回顶部 为什么要用函数 现在python届发生了一个大事件,len方法突然不能直接用了... 然后现在有一个需求,让你 ...
随机推荐
- pandas的分布式执行框架之modin
Scale your pandas workflows by changing one line of code To use Modin, replace the pandas impo ...
- Redis(REmote DIctionary Server)基础
Redis(REmote DIctionary Server)基础 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Redis是一个开放源代码(BSD许可)的内存数据结构存储,用作数 ...
- OS + CentOS / http_proxy / https_proxy / dalishangwang / repo
s OS + Linux RedHat / redhat7 / redhat 7 / redhat 6 / redhat 5 https://lindows.iteye.com/blog/456637 ...
- Subversion配置
1.下载Apache-Subversion-1.9.7:https://github.com/wangfajun/dev-tools 2.打开idea-->File-->Settings, ...
- 第十四节: EF的三种模式(四) 之 原生正宗的 CodeFirst模式的默认约定
一. 简介 1. 正宗的CodeFirst模式是不含有edmx模型,需要手动创建实体.创建EF上下文,然后生成通过代码来自动映射生成数据库. 2. 旨在:忘记SQL.忘记数据库. 3. 三类配置:On ...
- 纯css美化下拉框、复选框以及单选框样式并用jquery获取到其被选中的val
具体样式如图所示: 注:获取val值时记得要先引入jquery库奥. 1.下拉框 css部分 #cargo_type_id{ font-size: 13px; border: solid 1px #b ...
- Yii2.0连接多个数据库
Yii2.0连接多个数据库 一个项目根据需要会要求连接多个数据库,这里记录下实际项目中的操作流程.包括对数据库连接的配置以及如何生成模型文件,在控制器中加以运用. 一.配置 打开数据库配置文件c ...
- Leetcode#13. Roman to Integer(罗马数字转整数)
题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...
- 使用HBuilder+MUI+Flask后端服务器框架+Mongodb数据库开发手机APP
1.开发准备HBuilder: 1.下载安装HBuilder2.创建APP项目 3.代码示例: <!doctype html> <html lang="en"&g ...
- django drf 基础学习4
0 简介:介绍ModelViewSet基本使用规则1 views引用以及初始化 from rest_framework.viewsets import ModelViewSet clas ...