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方法突然不能直接用了... 然后现在有一个需求,让你 ...
随机推荐
- django和celery结合应用
django+celery项目结构 - project_name - app01 - __init__.py - admin.py - views.py - modes.py - tasks.py # ...
- 【C#】判断字符串中是否包含指定字符串,contains与indexof方法效率问题
#方法一:使用string.Contains方法 string.Contains是大小写敏感的,如果要用该方法来判断一个string是否包含某个关键字keyword,需要把这个string和这个key ...
- [物理学与PDEs]第1章第8节 静电场和静磁场 8.2 稳定电流的电场
1. 此时, Maxwell 方程组为 $$\beex \bea \Div{\bf D}&=\rho_f,\\ \rot {\bf E}&={\bf 0},\\ \Div{\bf B} ...
- 【OpenGL】搭建opgl环境
*GLFW+GLEW环境. 工具: GLFW库(下载) GLEW库 cMake软件(下载) 用cMake编译GLFW和GLEW成vs工程文件包,运行得到编译后文件. 在编译后文件夹中找到各个必需文件, ...
- 在Windows上安装Arduino-IDE
Arduino IDE的官方下载地址为:http://arduino.cc/en/Main/Software 也可以从我的网盘下载:win系统 1.8.9版本 链接:https://pan.baidu ...
- vue知多少,你对vue的认识比别人高在哪?
1.beforeCreated/created区别? beforeCreated钩子能干什么 2.data中使用props 3.get/set依赖收集 get收集依赖(观察者) set 观察者重新求值 ...
- 学习熟悉箭头函数, 类, 模板字面量, let和const声明
箭头函数:https://blog.csdn.net/qq_30100043/article/details/53396517 类:https://blog.csdn.net/pcaxb/articl ...
- C# - LINQ 语言集成查询
LINQ(Language Integrated Query) LINQ语言集成查询是一组用于C#语言的扩展.它允许编写C#代码对数据集进行查询,比如查询内存中的对象或查询远程数据库的表.利用linq ...
- Jumpserver(堡垒机)的安装与应用
官网:http://docs.jumpserver.org/zh/docs/introduce.html 作者:邓聪聪 环境 系统: CentOS 7.6 IP: 172.16.16.2 关闭 sel ...
- Beta 冲刺(1/7)
目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:beta冲刺(1/7) 团队部分 后敬甲(组长) 过去两天完成了哪些任务 团队完成测试答辩 整理博客 复习接口 接下来的 ...