【bytes】

英文文档:

class bytes([source[, encoding[, errors]]])

Return a new “bytes” object, which is an immutable sequence of integers in the range 0 <= x < 256bytes is an immutable version of bytearray – it has the same non-mutating methods and the same indexing and slicing behavior.

Accordingly, constructor arguments are interpreted as for bytearray().

说明:

1. 返回值为一个新的不可修改字节数组,每个数字元素都必须在0 - 255范围内,是bytearray函数的具有相同的行为,差别仅仅是返回的字节数组不可修改。

2. 当3个参数都不传的时候,返回长度为0的字节数组

>>> b = bytes()
>>> b
b''
>>> len(b)
0

3. 当source参数为字符串时,encoding参数也必须提供,函数将字符串使用str.encode方法转换成字节数组

>>> bytes('中文') #需传入编码格式
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
bytes('中文')
TypeError: string argument without an encoding
>>> bytes('中文','utf-8')
b'\xe4\xb8\xad\xe6\x96\x87'
>>> '中文'.encode('utf-8')
b'\xe4\xb8\xad\xe6\x96\x87'

4. 当source参数为整数时,返回这个整数所指定长度的空字节数组

>>> bytes(2)
b'\x00\x00'
>>> bytes(-2) #整数需大于0,用于做数组长度
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
bytes(-2)
ValueError: negative count

5. 当source参数为实现了buffer接口的object对象时,那么将使用只读方式将字节读取到字节数组后返回

6. 当source参数是一个可迭代对象,那么这个迭代对象的元素都必须符合0 <= x < 256,以便可以初始化到数组里

>>> bytes([1,2,3])
b'\x01\x02\x03'
>>> bytes([256,2,3])
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
bytes([256,2,3])
ValueError: bytes must be in range(0, 256)

7. 返回数组不可修改

>>> b = bytes(10)
>>> b
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> b[0]
0
>>> b[1] = 1 #不可修改
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
b[1] = 1
TypeError: 'bytes' object does not support item assignment >>> b = bytearray(10)
>>> b
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
>>> b[1] = 1 #可修改
>>> b
bytearray(b'\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00')

print(bytes([100,103,97])) #b'dga'
print(bytes('d','utf-8')) #b'd'
print([ord('d')]) #[100]
print(bytes([ord('d')])) #b'd'
 
 
【bytearray&bytes&str】
 
#bytes和bytearray 的区别就是,前者是不可变的,后者是可变的。【备注】同样不可变的还有str
#用法:单纯参数是数字,则表示长度,如果不是一个数字参数,则要加encoding。没有参数就是空的字节序列或字节数组
#经过memoryview()处理过的对象和对此对象的切片,这两个memory对象是不一样的,但这并不影响memoryview()实际的功能。 b = bytes()
print(b) #b''
print(len(b)) #0
#print(bytes('中文')) #TypeError: string argument without an encoding
print(bytes('中文','utf-8')) #b'\xe4\xb8\xad\xe6\x96\x87'
print('中文'.encode('utf-8')) #b'\xe4\xb8\xad\xe6\x96\x87' print(bytes(2)) #指定长度的空字节数组 b'\x00\x00'
#print(bytes(-2)) #ValueError: negative count print(bytes([1,2,3])) #b'\x01\x02\x03'
print(bytes([1]))
#print(bytes([256,4,7])) #ValueError: bytes must be in range(0, 256) b = bytes(10)
print(b) #b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
print(b[0]) #0
#b[1] = 1 #不可修改 TypeError: 'bytes' object does not support item assignment
b = bytearray(10)
print(b) #bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
b[1] = 1
print(b) # 可修改 bytearray(b'\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00') #不使用memoryview
a = 'aaaaaa'
b = a[:2] #会产生新的字符串
print(a)
print(b)
#b[:2] = 'bb' #TypeError: 'str' object does not support item assignment 【备注】string是一种不可变的数据类型 a = bytearray('aaaaaa','utf-8')
b = a[:2] #会产生新的bytearray
print(a) #bytearray(b'aaaaaa')
print(b) #bytearray(b'aa')
b[:2] = b'bb'
#对b的改动不影响a
print(a) #bytearray(b'aaaaaa')
print(b) #bytearray(b'bb') #使用memoryview
a = b'aaaaaa'
ma = memoryview(a) #memoryview()的参数在Python3中是bytes或bytearray
print(ma.readonly) #True
mb = ma[:2] #不会产生新的字符串
print(ma) #<memory at 0x0000000001FE5F48>
print(mb) #<memory at 0x00000000027C2048>
#mb[:2] = b'bb' #TypeError: cannot modify read-only memory 【备注】和对str不能做切片片的原因类似,都是不可变的 a = bytearray('aaaaaa','utf-8')
ma = memoryview(a)
print(ma.readonly) #False
mb = ma[:2] #不会产生新的bytearray
#不会产生新的bytearray并不代表mb这个对象就等于ma这个对象,实际的验证还要往下看
print(ma) #<memory at 0x0000000002202108>
print(mb) #<memory at 0x0000000000445F48>
mb[:2] = b'bb'
#不会产生新的bytearray的验证就在下面,对mb的改动就是对ma的改动
print(mb.tobytes()) #b'bb'
print(ma.tobytes()) #b'bbaaaa' 【引申】
typecode = 'd'
bytes1 = bytes([ord(typecode)])
import array
bytes2 = bytes(array.array(typecode,[3.0,4.0]))
print (bytes1)
print (bytes2)
cotets = bytes1 + bytes2
print (cotets)
memv = memoryview(cotets[1:]).cast(typecode)
print (memv)
print (memv.tobytes())
print (*memv)
import array
print (array.array('d',memv))
print (*[3.0,4.0]) '''
b'd'
b'\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@'
b'd\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@' b'\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@'
3.0 4.0
array('d', [3.0, 4.0])
3.0 4.0
''' #这里的Vector2d是我自定义的类,有__iter__()方法

【array】

1⃣️ 和list

【栗子】

import array
myarr = array.array('l', [8, 3, 6, 1])
print(myarr.tolist()) #[8, 3, 6, 1]
list1 = [3,6,8,9]
arr = array.array('l',list1)
print(arr) #array('l', [3, 6, 8, 9])

2⃣️枚举&常用方法

array使用方法:
Type code   C Type             Minimum size in bytes
        'c'         character                       1
        'b'         signed integer              1
        'B'         unsigned integer        1
        'u'         Unicode character       2
        'h'         signed integer            2
        'H'         unsigned integer         2
        'i'         signed integer            2
        'I'         unsigned integer         2
        'l'         signed integer            4
        'L'         unsigned integer         4
        'f'         floating point              4
        'd'         floating point              8
from array import *
创建一个interger类型的数组
myarr = array("l")  <--------创建数组
myarr.append(3)  <----------------追加元素
myarr.append(1)
myarr.append(8)

删除最后一个
myarr.pop()
删除第一个指定的X
myarr.remove(x)

取数组的值。。。。。通过下标
num1 = myarr[0]   <-----------第一个值

指定位置,。。。。插入值   

myarr.insert(6,10)  6表示下标。。。。10表示要插入的值

数组反序。。。。。
myarr.reverse()

【栗子】

import array

myarr = array.array('l')
print(myarr) #array('l')
myarr.append(3)
print(myarr) #array('l', [3])
myarr.append(1)
myarr.append(8)
print(myarr) #array('l', [3, 1, 8])
myarr.pop()
print(myarr) #array('l', [3, 1])
myarr.remove(3)
print(myarr) #array('l', [1])
myarr.append(3)
myarr.append(8)
num1 = myarr[0]
print(num1) #1
myarr.insert(1,6)
print(myarr) #array('l', [1, 6, 3, 8])
myarr.reverse()
print(myarr) #array('l', [8, 3, 6, 1])
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

【Python】【内置函数】【bytes&bytearray&str&array】的更多相关文章

  1. Python内置函数(13)——bytearray

    英文文档: class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray cla ...

  2. Python内置函数(7)——bytearray

    英文文档: class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray cla ...

  3. 第8.17节 Python __repr__方法和__str__方法、内置函数repr和str的异同点对比剖析

    一. 引言 记得刚开始学习Python学习字符串相关内容的时候,查了很多资料,也做了些测试,对repr和str这两个函数的返回值老猿一直没有真正理解,因为测试发现这两个函数基本上输出时一样的.到现在老 ...

  4. Python内置函数(12)——str

    英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string  ...

  5. Python内置函数(61)——str

    英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string ...

  6. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

  7. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  8. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

  9. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

  10. Python之路(第八篇)Python内置函数、zip()、max()、min()

    一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...

随机推荐

  1. Centos 设置zookeeper开机自启动

    把zookeeper做成服务 1.进入到/etc/rc.d/init.d目录下,新建一个zookeeper脚本 [root@zookeeper ~]# cd /etc/rc.d/init.d/ [ro ...

  2. 飞跃平野(sdut1124)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1124 飞跃原野 Time Limit: 500 ...

  3. Maven项目常见错误解决方法汇总

    issue 1.Java compiler level does not match the version of the installed Java project facet. 或者 One o ...

  4. [LeetCode] 237. Delete Node in a Linked List_Easy tag: Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  5. !! A股历史平均市盈率走势图

    http://value500.com/PE.asp 一. A股历史平均市盈率走势图 *数据来源:上海证券交易所 分享到: 354 - 上海A股 深圳A股更新时间 2017年6月7日 2017年6月7 ...

  6. Centos下添加PHP对MSSQL的支持

    Leave a reply 其实很少会有连接SQL Server的机会,不过我们公司刚好有个应用需要使用的SQL Server的数据库,所以也知道给LNMP安装MSSQL的扩展. 搜索网上的相关文章一 ...

  7. LeetCode-EvaluteReversePolishNotation

    题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...

  8. CATiledLayer

    CATiledLayer 有些时候你可能需要绘制一个很大的图片,常见的例子就是一个高像素的照片或者是地球表面的详细地图.iOS应用通畅运行在内存受限的设备上,所以读取整个图片到内存中是不明智的.载入大 ...

  9. redis安装及注意事项

    在linux中使用wget时,若报-bash: wget: command not found,则表明没有安装wget,需要安装,安装命令如下: yum -y install wget 安装完成即可以 ...

  10. MongoDB ----基于分布式文件存储的数据库

    参考: http://www.cnblogs.com/huangxincheng/category/355399.html http://www.cnblogs.com/daizhj/category ...