bytes数组转string指定编码_一篇文章弄懂Python中所有数组数据类型
前言
數(shù)組類型是各種編程語言中基本的數(shù)組結(jié)構(gòu)了,本文來盤點(diǎn)下Python中各種“數(shù)組”類型的實(shí)現(xiàn)。
- list
- tuple
- array.array
- str
- bytes
- bytearray
其實(shí)把以上類型都說成是數(shù)組是不準(zhǔn)確的。這里把數(shù)組當(dāng)作一個(gè)廣義的概念,即把列表、序列、數(shù)組都當(dāng)作array-like數(shù)據(jù)類型來理解。
注意本文所有代碼都是在Python3.7中跑的^_^
0x00 可變的動(dòng)態(tài)列表list
list應(yīng)該是Python最常用到的數(shù)組類型了。它的特點(diǎn)是可變的、能動(dòng)態(tài)擴(kuò)容,可存儲(chǔ)Python中的一切對(duì)象,使用時(shí)不用指定存儲(chǔ)的元素的類型。
使用非常簡(jiǎn)單
>>>0x01 不可變的tuple
tuple的操作與list類似。它的特點(diǎn)是不可變,不能擴(kuò)容,可存儲(chǔ)Python中的一切對(duì)象,使用時(shí)不用指定存儲(chǔ)的元素的類型。
>>> t = 'one','two',3 >>> t ('one', 'two', 3) >>> t.append(4) AttributeError: 'tuple' object has no attribute 'append' >>> del t[0] TypeError: 'tuple' object doesn't support item deletiontuple可以使用+運(yùn)算符,這個(gè)運(yùn)算將創(chuàng)建一個(gè)新的tuple對(duì)象用于存儲(chǔ)數(shù)據(jù)。
>>> t+(1,) ('one', 'two', 3, 1) >>> tcopy = t+(1,) >>> tcopy ('one', 'two', 3, 1) >>> id(tcopy) 4604415336 >>> id(t) 4605245696可以看出tuple執(zhí)行+運(yùn)算符之后兩個(gè)對(duì)象的地址是不一樣
0x02 array.array
如果在Python中要用到其它語言中類似“數(shù)組”的數(shù)據(jù)結(jié)構(gòu),就需要用到array模塊了。它的特點(diǎn)是可變的、存儲(chǔ)相同類型的數(shù)值,不能存儲(chǔ)對(duì)象。
因?yàn)閍rray在使用的時(shí)候要指定元素?cái)?shù)據(jù)類型,因此它比list和tuple都有比較高效空間性能。
# 使用時(shí)指定元素?cái)?shù)據(jù)類型為`float` >>> arr = array.array('f', (1.0, 1.5, 2.0, 2.5)) >>> arr array('f', [1.0, 1.5, 2.0, 2.5]) # 修改一個(gè)元素 >>> arr[1]=12.45 >>> arr array('f', [1.0, 12.449999809265137, 2.0, 2.5]) # 刪除一個(gè)元素 >>> del arr[2] >>> arr array('f', [1.0, 12.449999809265137, 2.5]) # 增加一個(gè)元素 >>> arr.append(4.89) >>> arr array('f', [1.0, 12.449999809265137, 2.5, 4.889999866485596]) # 如果將一個(gè)字符串類型數(shù)據(jù)存儲(chǔ)到一個(gè)浮點(diǎn)數(shù)的數(shù)組將會(huì)報(bào)錯(cuò) >>> arr[0]='hello' TypeError: must be real number, not strarray中元素的數(shù)據(jù)類型可以參考下表
0x03 字符串序列str
Python3中使用str對(duì)象來表示一個(gè)文本字符序列(看,這跟Java中的字符串String是多么相似呢)。它的特點(diǎn)不可變的Unicode字符序列。
在str中它的每一個(gè)元素都是字符串對(duì)象。
>>> s ='123abc' >>> s '123abc' >>> s[0] '1' >>> s[2] '3' # 字符串是不可變的序列,不能刪除其中的元素 >>> del s[1] TypeError: 'str' object doesn't support item deletion # 要對(duì)字符串進(jìn)行操作,可以轉(zhuǎn)化成list >>> sn = list(s) >>> sn ['1', '2', '3', 'a', 'b', 'c'] >>> sn.append(9) >>> sn ['1', '2', '3', 'a', 'b', 'c', 9] # 字符串中的元素也是字符串對(duì)象 >>> type(s[2]) <class 'str'> >>> type(s) <class 'str'>str對(duì)象也可以執(zhí)行+操作,它也會(huì)生成一個(gè)新對(duì)象用于存儲(chǔ)。
>>> s2 = s+'33' >>> s2 '123abc33' >>> id(s2) 4605193648 >>> id(s) 45526404160x04 bytes
bytes對(duì)象用于存儲(chǔ)字節(jié)序列,它的特點(diǎn)是不可變存儲(chǔ),可存儲(chǔ)0-256的數(shù)值。
>>> b = bytes([0,2,4,8]) >>> b[2] 4 >>> b b'x00x02x04x08' >>> b[0]=33 TypeError: 'bytes' object does not support item assignment >>> del b[0] TypeError: 'bytes' object doesn't support item deletion0x05 bytearray
bytearray對(duì)象與bytes類似,用于存儲(chǔ)字節(jié)序列。它的特點(diǎn)是可變的,能動(dòng)態(tài)擴(kuò)容的字節(jié)數(shù)組。
>>> ba = bytearray((1,3,5,7,9)) >>> ba bytearray(b'x01x03x05x07t') >>> ba[1] 3 # 刪除一個(gè)元素 >>> del ba[1] >>> ba bytearray(b'x01x05x07t') >>> ba[0]=2 >>> ba[0] 2 # 添加一個(gè)元素 >>> ba.append(6) # 只能添加字節(jié) >>> ba.append(s) TypeError: 'str' object cannot be interpreted as an integer >>> ba bytearray(b'x02x05x07tx06') # 字節(jié)的范圍是0-256 >>> ba[2]=288 ValueError: byte must be in range(0, 256)bytearray可以轉(zhuǎn)化成bytes對(duì)象,但效率不是很高。
# bytearray轉(zhuǎn)成bytes將生成一個(gè)新對(duì)象 >>> bn = bytes(ba) >>> id(bn) 4604114344 >>> id(ba) 45524735440x06 各個(gè)類型相互轉(zhuǎn)化
tuple->list
>>> tuple(l) ('a', 'b', 'c')list->tuple
>>> t ('a', 'b', 'c') >>> list(t) ['a', 'b', 'c']str->list
>>> l = list('abc') >>> l ['a', 'b', 'c']list->str
>>> l ['a', 'b', 'c'] >>> ''.join(l) 'abc'str->bytes
>>> s = '123' >>> bytes(s) TypeError: string argument without an encoding >>> bytes(s,encoding='utf-8') b'123' # 或者使用str的encode()方法 >>> s.encode() b'123'bytes->str
>>> b = b'124' >>> b b'124' >>> type(b) <class 'bytes'> >>> str(b,encoding='utf-8') '124' # 或使用bytes的decode() >>> b.decode() '124'0x07 總結(jié)
這些數(shù)據(jù)類型都是Python自帶的,在實(shí)際開發(fā)中應(yīng)該根據(jù)具體需求選擇合適的數(shù)據(jù)類型。例如當(dāng)要存儲(chǔ)的元素類型是多種多樣的,那么就應(yīng)該使用list或者tuple。而array.array相對(duì)來說擁有較好的空間性能,但它只能存儲(chǔ)單一類型。
我相信在很多業(yè)務(wù)場(chǎng)景中l(wèi)ist或tuple是可以滿足需求的,只是其它數(shù)據(jù)結(jié)構(gòu)也要有所了解,在我們做一些基礎(chǔ)組件時(shí),會(huì)考慮數(shù)據(jù)結(jié)構(gòu)的性能,或者閱讀他人的代碼時(shí),能做到心中有數(shù)。
總結(jié)
以上是生活随笔為你收集整理的bytes数组转string指定编码_一篇文章弄懂Python中所有数组数据类型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mfc try catch 捕获并显示_
- 下一篇: java api math_JAVA 函