【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. [dt]世纪历史长河年代表

    年代口诀 夏商与西周, 东周分两段, 春秋和战国, 一统秦两汉, 三分魏蜀吴, 二晋前后延, 南北朝并列, 隋唐五代传, 宋元明清后, 皇朝至此完. 中国历史长河年代表 参考: 中国历史朝代顺序表.年 ...

  2. Java中将xml文件转化为json的两种方式

    原文地址https://blog.csdn.net/a532672728/article/details/76312475 最近有个需求,要将xml转json之后存储在redis中,找来找去发现整体来 ...

  3. liferay笑傲江湖-API之参数的工具类(ParamUtil)

    public class ParamUtil { 036 037 public static boolean get( 038 HttpServletRequest request, String p ...

  4. webapi 返回json及route设置

    1.返回json 修改App_Start/webapiconfig public static void Register(HttpConfiguration config) { // Web API ...

  5. XDU1024简单逆序对(贪心||分治)

    题目描述 逆序对问题对于大家来说已经是非常熟悉的问题了,就是求i<j时,a[i] > a[j]的组数.现在请你求出一串数字中的逆序对的个数,需要注意的是,这些数字均在[0,9]之内. 输入 ...

  6. 测试开发-web测试要点

    参数输入考虑 参数数值包含1个.多个.很多个.null.参数值前后包含空格的2种情况   数字类型:正数.负数.0.0.0.+0.0.-0.0.指数.对数.分数.小数.复数.科学计数法的测试,全角的数 ...

  7. thinkphp标签实现bootsrtap轮播carousel实例

    thinkphp标签实现bootsrtap轮播carousel实例由于轮播carousel第一个div需要设置active样式才能正常显示,上面的圆点也同样需要数字,使用volist标签在循环的同时可 ...

  8. qmake使用方法(自动生成Makefile文件)

    qmake的使用简介 下面是qmake的简单介绍和使用要领,更为详细的信息请参阅手册 qmake的介绍 手写Makefile是比较困难并且容易出错的,尤其是需要给不同的平台和编译器组合写几个Makef ...

  9. 如何加固linux NFS 服务安全的方法

    NFS(Network File System)是 FreeBSD 支持的一种文件系统,它允许网络中的计算机之间通过 TCP/IP 网络共享资源.不正确的配置和使用 NFS,会带来安全问题. 概述 N ...

  10. android 接受系统锁屏广播,及高版本发送广播

    protected BroadcastReceiver messageReceiver = new BroadcastReceiver() { @Override public void onRece ...