An article on understanding all array data types in Python

  • 2021-06-29 11:34:05
  • OfStack

Preface

Array type is the basic array structure in various programming languages. This article will review the implementation of various Array types in Python.

list tuple array.array str bytes bytearray

In fact, it is inaccurate to say that all the above types are arrays.Arrays are treated as a general concept here, that is, lists, sequences, and arrays are all understood as array-like data types.

Note that all code in this article is ^^ running in Python3.7

0x00 Variable Dynamic List list

list should be the most commonly used array type for Python.It is variable, dynamically expandable, and can store all objects in Python without specifying the type of elements stored.

Very easy to use


>>> arr = ["one","two","three"]
>>> arr[0]
'one'
#  Dynamic Capacity Expansion 
>>> arr.append(4)
>>> arr
['one', 'two', 'three', 4]
#  delete 1 Elements 
>>> del arr[2]
>>> arr
['one', 'two', 4]

0x01 Immutable tuple

tuple operates similarly to list.It is characterized by immutability, inability to expand, and can store all objects in Python without specifying the type of elements stored.


>>> 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 can use the + operator, which creates a new tuple object to store data.


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

You can see that after tuple executes the + operator, the addresses of the two objects are different

0x02 array.array

If you want to use data structures like Arrays in other languages in Python, you need to use the array module.It is characterized by variable, stored values of the same type, and cannot store objects.

Because array specifies element data types when used, it has more efficient spatial performance than list and tuple.


#  Specify element data type when using `float`
>>> arr = array.array('f', (1.0, 1.5, 2.0, 2.5))
>>> arr
array('f', [1.0, 1.5, 2.0, 2.5])
#  modify 1 Elements 
>>> arr[1]=12.45
>>> arr
array('f', [1.0, 12.449999809265137, 2.0, 2.5])
#  delete 1 Elements 
>>> del arr[2]
>>> arr
array('f', [1.0, 12.449999809265137, 2.5])
#  increase 1 Elements 
>>> arr.append(4.89)
>>> arr
array('f', [1.0, 12.449999809265137, 2.5, 4.889999866485596])
#  If the 1 String type data stored in 1 Array of floating point numbers will fail 
>>> arr[0]='hello'
TypeError: must be real number, not str

The data types of elements in array can be referenced in the following table

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 String Sequence str

The str object is used in Python3 to represent a text character sequence (see how similar this is to the string String in Java).It is characterized by an immutable sequence of Unicode characters.

Every element of str is a string object.


>>> s ='123abc'
>>> s
'123abc'
>>> s[0]
'1'
>>> s[2]
'3'
#  Strings are immutable sequences and cannot be deleted 
>>> del s[1]
TypeError: 'str' object doesn't support item deletion 
#  To manipulate strings, you can convert them to list 
>>> sn = list(s)
>>> sn
['1', '2', '3', 'a', 'b', 'c']
>>> sn.append(9)
>>> sn
['1', '2', '3', 'a', 'b', 'c', 9]
#  Elements in strings are also string objects 
>>> type(s[2])
<class 'str'>
>>> type(s)
<class 'str'>

The str object can also perform a + operation, and it generates a new object for storage.


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

0x04 bytes

The bytes object is used to store a sequence of bytes. It is characterized by immutable storage, which 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

The bytearray object is similar to bytes in that it stores a sequence of bytes.It is characterized by variable, dynamically expandable byte arrays.


>>> ba = bytearray((1,3,5,7,9))
>>> ba
bytearray(b'\x01\x03\x05\x07\t')
>>> ba[1]
3
#  delete 1 Elements 
>>> del ba[1]
>>> ba
bytearray(b'\x01\x05\x07\t')
>>> ba[0]=2
>>> ba[0]
2
#  Add to 1 Elements 
>>> ba.append(6)
#  Only bytes can be added 
>>> ba.append(s)
TypeError: 'str' object cannot be interpreted as an integer
>>> ba
bytearray(b'\x02\x05\x07\t\x06')
#  The range of bytes is 0-256
>>> ba[2]=288
ValueError: byte must be in range(0, 256)

bytearray can be converted into an bytes object, but it is not very efficient.


# bytearray Conversion bytes Will generate 1 New Objects 
>>> bn = bytes(ba)
>>> id(bn)
4604114344
>>> id(ba)
4552473544

0x06 Types Convert to each other
tuple- > list


>>> tuple(l)
('a', 'b', 'c')

list- > tuple


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

str- > list


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

list- > str


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

str- > bytes


>>> s = '123'
>>> bytes(s)
TypeError: string argument without an encoding
>>> bytes(s,encoding='utf-8')
b'123'
#  Or use str Of encode() Method 
>>> s.encode()
b'123'

bytes- > str


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

Summary of 0x07

These data types come with Python, so the appropriate data types should be selected according to the specific requirements in the actual development.For example, when there are many different types of elements to store, you should use list or tuple.array.array has relatively good spatial performance, but it can only store single-1 types.
I believe that in many business scenarios, list or tuple can meet your needs, but other data structures need to be understood. When we do some basic components, we think about the performance of the data structure, or read someone else's code, and we know what to do.

0x08 Learning Materials

docs.python.org/3.1/library... docs.python.org/zh-cn/3/lib... docs.python.org/3/library/s...

Related articles: