Array types in various programming languages is the basic structure of the array, the inventory herein to Python
various "array" type of implementation.
list
tuple
array.array
str
bytes
bytearray
In fact, the above said as an array type is not accurate. Here the array as a broad concept, ie the list, the sequence, as are an array ofarray-like
data types to understand.
(I want to learn programming from a small partner search circle T community , more and more industry-related industry information about free video tutorials. Oh, absolutely free!)
Note This article All code is in the Python3.7
running in _
0x00 variable dynamic list of list
list
It should be Python
an array of the most commonly used type. It is characterized by variable, dynamically expansion, may store Python
all the object without specifying the type of storage element during use.
Very easy to use
>>> arr = ["one","two","three"]
>>> arr[0]
'one'
# 动态扩容
>>> arr.append(4)
>>> arr
['one', 'two', 'three', 4]
# 删除一个元素
>>> del arr[2]
>>> arr
['one', 'two', 4]
0x01 immutable tuple
tuple
The operation list
is similar. It is characterized immutable, not expansion, may store Python
all the object without specifying the type of storage element during use.
>>> 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
You can use +
the operator, the operator will create a new tuple
object is used to store data.
>>> t+(1,)
('one', 'two', 3, 1)
>>> tcopy = t+(1,)
>>> tcopy
('one', 'two', 3, 1)
>>> id(tcopy)
4604415336
>>> id(t)
4605245696
It can be seen tuple address after performing two objects is not the same operator +
0x02 array.array
If you Python
are to use other languages like "array" data structure, we need to use array
the module up. It is characterized by a variable, the value of the same type storage, can not store objects.
Because array
when the element used to specify the data type, so it is more than list
and tuple
has a more space-efficient performance.
# 使用时指定元素数据类型为`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
Data type array elements can refer to the table
Type code | C Type | Python Type |
---|---|---|
‘b’ | signed char | int |
‘B’ | unsigned char | int |
'In' | 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 sequence string str
Python3
Use str
objects to represent a text character sequence (see this with Java
strings String
how similar is it). It features immutable Unicode
sequence of characters.
In str
a String object that is each element.
>>> 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 object can also perform operation, it also generates a new object for storage.
>>> s2 = s+'33'
>>> s2
'123abc33'
>>> id(s2)
4605193648
>>> id(s)
4552640416
0x04 bytes
bytes object for storing a sequence of bytes, which is characterized by non-change memory can store values of 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
Objects with bytes
similar, for storing a sequence of bytes. It is characterized by variable, dynamically expansion byte array.
>>> 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
It can be converted into bytes
an object, but not very efficient.
# bytearray转成bytes将生成一个新对象
>>> bn = bytes(ba)
>>> id(bn)
4604114344
>>> id(ba)
4552473544
0x06 each type interconversion
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 summary
These data types are Python
built-in, should select the appropriate data type in accordance with the specific needs of the actual development. For example, when the element type to be stored is diverse, then it should be used list
or tuple
. And array.array
relatively speaking, have better performance space, but it can only store a single type.
I believe in many business scenarios list
or tuple
when is to meet the needs, but also to understand other data structures, when we do some basic components, consider the performance data structures, or read other people's code can do hearts There are several.