python的array初识
2017-07-25 15:10
281 查看
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初识1
- [Leetcode][python]Merge Sorted Array
- python中的list以及list与array相互转换
- leetcode 238. Product of Array Except Self (Python版)
- 初识python PIL与pytesser
- 初识Python
- 180206—【Python 初识 D13】字符串序列:切片、字符串不变性
- leetcode 665. Non-decreasing Array python
- Python中的二维数组实例(list与numpy.array)
- python学习笔记六之初识面向对象上(基础篇)
- [leetcode: Python]189. Rotate Array
- python 之 前端初识 html
- 【leetcode】189. Rotate Array(Python & C++)
- Python初识
- [leetcode]Search in Rotated Sorted Array II @ Python
- Python进阶(二)-初识Python数据元素:字典&时间
- 初识Python---感受蟒蛇的强大(一)
- Head First Python之人人都爱列表(1-初识Python)
- python中array的sum()用法
- python中numpy库matrix和array的融合使用