【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. redis 在windows 上面的安装和使用,集群搭建

    redis作为nosql数据库,将数据存储到内存中(缓存),具有非常高的性能.下面讲解一下redis的安装及java api的使用. 1:redis 安装 windows 上面直接下载msi文件,安装 ...

  2. AMR格式语音采集/编码/转码/解码/播放

    1.opencore-amr源码下载 https://sourceforge.net/projects/opencore-amr/files/opencore-amr/ 2.opencore-amr编 ...

  3. poj2524(简单并查集)

    #include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>u ...

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

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

  5. vmware tool安装

    https://www.vmware.com/support/ws55/doc/ws_newguest_tools_linux.html VMware Workstation 5.5 Features ...

  6. Amaze UI JS 气泡弹出

    http://amazeui.org/javascript/popover?_ver=2.x

  7. 20154312 曾林 Exp4恶意软件分析

    写在前面 如果把恶意软件比作罪犯的话,怎么看这次实验? 实验目的:以后能够在茫茫人海中找到罪犯. 实验过程:现在以及抓到了一个罪犯,把他放到茫茫人海里去,看看他和普通人有啥区别.这些区别就是罪犯的特征 ...

  8. Java HTTP通信--Get与POST请求

    一.JDK自带的http通信机制--java.net.URL package com.wjy; import java.io.BufferedReader; import java.io.Buffer ...

  9. python插入排序算法总结

    插入排序算法总结: 插入算法的核心是 每次循环到一个数时,都认为这个数之前的数列都是排好序的,将一个数插入到已经排好序的有序数列中,从而得到一个新的.个数加一的有序数列. 过程:从第一个元素开始,第一 ...

  10. python webdriver api-读取、设置配置文件

    文件结构: db.ini放置db信息的配置文件 文件中[gloryroad]是section信息 下边的dbname等是option信息 UiObjectMap.ini放置访问web的配置信息 配置用 ...