一篇文章弄懂Python中所有數組數據類型

這篇文章主要給大家介紹了關於Python中所有數組數據類型的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

前言

數組類型是各種編程語言中基本的數組結構了,本文來盤點下Python中各種“數組”類型的實現。

  • list
  • tuple
  • array.array
  • str
  • bytes
  • bytearray

其實把以上類型都說成是數組是不準確的。這裏把數組當作一個廣義的概念,即把列表、序列、數組都當作array-like數據類型來理解。

注意本文所有代碼都是在Python3.7中跑的^_^

0x00 可變的動態列表list

list應該是Python最常用到的數組類型了。它的特點是可變的、能動態擴容,可存儲Python中的一切對象,使用時不用指定存儲的元素的類型。

使用非常簡單

>>> arr = ["one","two","three"]
>>> arr[0]
'one'
# 動態擴容
>>> arr.append(4)
>>> arr
['one', 'two', 'three', 4]
# 刪除一個元素
>>> del arr[2]
>>> arr
['one', 'two', 4]

0x01 不可變的tuple

tuple的操作與list類似。它的特點是不可變,不能擴容,可存儲Python中的一切對象,使用時不用指定存儲的元素的類型。

>>> 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 deletion

tuple可以使用+運算符,這個運算將創建一個新的tuple對象用於存儲數據。

>>> t+(1,)
('one', 'two', 3, 1)
>>> tcopy = t+(1,)
>>> tcopy
('one', 'two', 3, 1)
>>> id(tcopy)
4604415336
>>> id(t)
4605245696

可以看出tuple執行+運算符之後兩個對象的地址是不一樣

0x02 array.array

如果在Python中要用到其它語言中類似“數組”的數據結構,就需要用到array模塊了。它的特點是可變的、存儲相同類型的數值,不能存儲對象。

因爲array在使用的時候要指定元素數據類型,因此它比list和tuple都有比較高效空間性能。

# 使用時指定元素數據類型爲`float`
>>> arr = array.array('f', (1.0, 1.5, 2.0, 2.5))
>>> arr
array('f', [1.0, 1.5, 2.0, 2.5])
# 修改一個元素
>>> arr[1]=12.45
>>> arr
array('f', [1.0, 12.449999809265137, 2.0, 2.5])
# 刪除一個元素
>>> del arr[2]
>>> arr
array('f', [1.0, 12.449999809265137, 2.5])
# 增加一個元素
>>> arr.append(4.89)
>>> arr
array('f', [1.0, 12.449999809265137, 2.5, 4.889999866485596])
# 如果將一個字符串類型數據存儲到一個浮點數的數組將會報錯
>>> arr[0]='hello'
TypeError: must be real number, not str

array中元素的數據類型可以參考下表

Type code C Type Python Type
'b' signed char int
'B' unsigned char int
'u' Py_UNICODE Unicode character
'h' signed short int
'H' unsigned short int
'i' signed int int
'I' unsigned int int
'l' signed long int
'L' unsigned long int
'q' signed long long int
'Q' unsigned long long int
'f' float float
'd' double float

0x03 字符串序列str

Python3中使用str對象來表示一個文本字符序列(看,這跟Java中的字符串String是多麼相似呢)。它的特點不可變的Unicode字符序列。

在str中它的每一個元素都是字符串對象。

>>> s ='123abc'
>>> s
'123abc'
>>> s[0]
'1'
>>> s[2]
'3'
# 字符串是不可變的序列,不能刪除其中的元素
>>> del s[1]
TypeError: 'str' object doesn't support item deletion 
# 要對字符串進行操作,可以轉化成list 
>>> sn = list(s)
>>> sn
['1', '2', '3', 'a', 'b', 'c']
>>> sn.append(9)
>>> sn
['1', '2', '3', 'a', 'b', 'c', 9]
# 字符串中的元素也是字符串對象
>>> type(s[2])
<class 'str'>
>>> type(s)
<class 'str'>

str對象也可以執行+操作,它也會生成一個新對象用於存儲。

>>> s2 = s+'33'
>>> s2
'123abc33'
>>> id(s2)
4605193648
>>> id(s)
4552640416

0x04 bytes

bytes對象用於存儲字節序列,它的特點是不可變存儲,可存儲0-256的數值。

>>> b = bytes([0,2,4,8])
>>> b[2]
4
>>> b
b'\x00\x02\x04\x08'
>>> b[0]=33
TypeError: 'bytes' object does not support item assignment
>>> del b[0]
TypeError: 'bytes' object doesn't support item deletion

0x05 bytearray

bytearray對象與bytes類似,用於存儲字節序列。它的特點是可變的,能動態擴容的字節數組。

>>> ba = bytearray((1,3,5,7,9))
>>> ba
bytearray(b'\x01\x03\x05\x07\t')
>>> ba[1]
3
# 刪除一個元素
>>> del ba[1]
>>> ba
bytearray(b'\x01\x05\x07\t')
>>> ba[0]=2
>>> ba[0]
2
# 添加一個元素
>>> ba.append(6)
# 只能添加字節
>>> ba.append(s)
TypeError: 'str' object cannot be interpreted as an integer
>>> ba
bytearray(b'\x02\x05\x07\t\x06')
# 字節的範圍是0-256
>>> ba[2]=288
ValueError: byte must be in range(0, 256)

bytearray可以轉化成bytes對象,但效率不是很高。

# bytearray轉成bytes將生成一個新對象
>>> bn = bytes(ba)
>>> id(bn)
4604114344
>>> id(ba)
4552473544

0x06 各個類型相互轉化
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 總結

這些數據類型都是Python自帶的,在實際開發中應該根據具體需求選擇合適的數據類型。例如當要存儲的元素類型是多種多樣的,那麼就應該使用list或者tuple。而array.array相對來說擁有較好的空間性能,但它只能存儲單一類型。
我相信在很多業務場景中list或tuple是可以滿足需求的,只是其它數據結構也要有所瞭解,在我們做一些基礎組件時,會考慮數據結構的性能,或者閱讀他人的代碼時,能做到心中有數。

0x08 學習資料

好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對神馬文庫的支持。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章