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初识的更多相关文章

  1. codefroce D. Powerful array[初识块状数组]

    codefroce D. Powerful array[初识块状数组] 由于是初始所以,仅仅能先用别人的分析.囧... 题目: 给定一个数列:A1, A2,--,An,定义Ks为区间(l,r)中s出现 ...

  2. 孤荷凌寒自学python第二十一天初识python的类

    孤荷凌寒自学python第二十一天初识python的类 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 类是面向对象的编程语言非常重要的概念. 编程语言的进化史中从顺序编程到结构化编程,最后才 ...

  3. python numpy array 与matrix 乘方

    python numpy array 与matrix 乘方 编程语言 waitig 1年前 (2017-04-18) 1272℃ 百度已收录 0评论 数组array 的乘方(**为乘方运算符)是每个元 ...

  4. Python 入门 之 初识面向对象

    Python 入门 之 初识面向对象 1.初识面向对象编程 (核心--对象) (1)观察以下代码: # 面向过程编程 s = "alexdsb" count = 0 for i i ...

  5. python函数-函数初识

    python函数-函数初识 1.函数的定义 语法 def 函数名(参数1,参数2,参数3,...): '''注释''' 函数体 return 返回的值 2.函数的使用原则---先定义后调用 #定义阶段 ...

  6. 01----python入门----python安装与初识

    ----python入门----python安装与初识 python是一门编程语言,作为学习python的开始,需要事先搞明白:编程的目的是什么?什么是编程语言?什么是编程? 这是一个很好的开头!但是 ...

  7. Python之路【第五篇】python基础 之初识函数(一)和文件管理

    转载请注明出处http://www.cnblogs.com/wupeiqi/articles/5453708.html 函数 一.背景                                 ...

  8. python numpy array 的一些问题

    1 将list转换成array 如果list的嵌套数组是不规整的,如 a = [[1,2], [3,4,5]] 则a = numpy.array(a)之后 a的type是ndarray,但是a中得元素 ...

  9. python之路——初识函数

    阅读目录 为什么要用函数 函数的定义与调用 函数的返回值 函数的参数 本章小结 返回顶部 为什么要用函数 现在python届发生了一个大事件,len方法突然不能直接用了... 然后现在有一个需求,让你 ...

随机推荐

  1. 20165232 实现pwd

    20165232 实现mypwd 题目要求 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 实现mypwd 测试mypwd 学习pwd命令 用man pwd 查看 ...

  2. 源码来袭:bind手写实现

    JavaScript中的this指向规则 源码来袭:call.apply手写实现与应用 理解建议:如果对this指向规则不了解的话,建议先了解this指向规则,最好还能对call和apply的使用和内 ...

  3. SCI,EI,ISTP

    SCI:   Science Citation Index EI:     The Engineering Index ISTP:  Index to Scientific & Technic ...

  4. StringBuffer/StringBuilder总结

  5. 弄懂promise

    ECMAscript 6 原生提供了 Promise 对象. Promise 对象代表了未来将要发生的事件,用来传递异步操作的消息 有了 Promise 对象,就可以将异步操作以同步操作的流程表达出来 ...

  6. Git可视化教程——Git Gui的使用

    参考链接:https://blog.csdn.net/qq_34842671/article/details/70916587

  7. POJ 1269 Intersecing Lines (直线相交)

    题目: Description We all know that a pair of distinct points on a plane defines a line and that a pair ...

  8. setTimeout 第三个参数秒懂

    好吧,假设你们都是从 ES6 里 promise 发现 setTimeout 还有第三个参数的,下面讲讲到底是干嘛的 setTimeout 第三个及之后的参数作用:定时器启动时候,第三个以后的参数是作 ...

  9. Spring-boot 国际化

    在application.properties文件中配置 spring.message.basename=i18n.login 页面使用 th:text="#{login.tip}" ...

  10. Python-Django 模型层-多表查询-2

    -related_name:基于双下划线的跨表查询,修改反向查询的字段 -related_query_name:基于对象的跨表查询,修改反向查询字段 publish = ForeignKey(Blog ...