As a little programmer in my programming field, I am currently working as team lead in an entrepreneurial team. The technology stack involves Android, Python, Java and Go, which is also the main technology stack of our team. Contact: [email protected]
Array types are the basic array structures in various programming languages. In this article, we will take a look at Python’s implementation of various “array” types.
list
tuple
array.array
str
bytes
bytearray
It is not accurate to call all of the above types arrays. Arrays are thought of as a broad concept. Lists, sequences, and arrays are all understood as array-like data types.
Note that all the code in this article is inPython3.7
Run the ^_^
0x00 Mutable dynamic list List
List is probably the most commonly used array type in Python. It is mutable, dynamically scalable, and can store all objects in Python without specifying the type of element to store.
Very simple to use
>>> arr = ["one"."two"."three"]
>>> arr[0]
'one'
# Dynamic capacity expansion
>>> arr.append(4)
>>> arr
['one'.'two'.'three'And 4)Delete an element
>>> del arr[2]
>>> arr
['one'.'two'And 4)Copy the code
0x01 Immutable Tuple
Tuple operates like a list. It is immutable and cannot be expanded. It can store all objects in Python without specifying the type of element to store.
>>> 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
Copy the code
A tuple can use the + operator, which creates a new tuple object to store data in.
>>> t+(1,)
('one'.'two', 3, 1)
>>> tcopy = t+(1,)
>>> tcopy
('one'.'two', 3, 1)
>>> id(tcopy)
4604415336
>>> id(t)
4605245696
Copy the code
You can see that the addresses of the two objects are different after tuple executes the + operator
0x02 array.array
If you want to use data structures like “arrays” in other languages in Python, you need the array module. It is mutable and stores values of the same type, not objects.
Because array is used with specified element data types, it has more efficient spatial performance than both lists and tuples.
Specify element data type as' float 'when used
>>> arr = array.array('f', (1.0, 1.5, 2.0, 2.5)) >>> ARR Array ('f', [1.0, 1.5, 2.0, 2.5])
# modify an element>>> ARR [1]=12.45 >>> ARR Array ('f', [1.0, 12.449999809265137, 2.0, 2.5])
Delete an element
>>> del arr[2]
>>> arr
array('f'And [1.0, 12.449999809265137, 2.5])Add an element>>> Arr.appEnd (4.89) >>> ARr array('f', [1.0, 12.449999809265137, 2.5, 4.889999866485596])
An error will be reported if you store string data in an array of floating point numbers
>>> arr[0]='hello'
TypeError: must be real number, not str
Copy the code
The following table lists the data types of the elements in the 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 String sequence STR
Python3 uses STR objects to represent a sequence of text characters (see how similar this is to the String in Java). It features immutable Unicode character sequences.
Each of its elements in STR is a string object.
>>> s ='123abc'
>>> s
'123abc'
>>> s[0]
'1'
>>> s[2]
'3'
# strings are immutable sequences in which elements cannot be deleted
>>> del s[1]
TypeError: 'str' object doesnDeletion # delete from list >>> sn = list(s) >>> sn ['1', '2', '3', 'a', 'b', 'c']
>>> sn.append(9)
>>> sn
['1', '2', '3', 'a', 'b', 'c>>> type(s[2]) str'>
>>> type(s)
<class 'str'>
Copy the code
The STR object can also perform the + operation, which also generates a new object for storage.
>>> s2 = s+'33'
>>> s2
'123abc33'
>>> id(s2)
4605193648
>>> id(s)
4552640416
Copy the code
0x04 bytes
Bytes objects are used to store sequences of bytes and are characterized by immutable storage that can store values from 0 to 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
Copy the code
0x05 bytearray
The bytearray object, similar to bytes, is used to store sequences of bytes. It features a mutable, dynamically scalable byte array.
> > > ba = bytearray containing (,3,5,7,9) (1) > > > ba bytearray containing (b'\x01\x03\x05\x07\t')
>>> ba[1]
3
Delete an element
>>> del ba[1]
>>> ba
bytearray(b'\x01\x05\x07\t')
>>> ba[0]=2
>>> ba[0]
2
Add an element
>>> 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')
# bytes range from 0 to 256
>>> ba[2]=288
ValueError: byte must be in range(0, 256)
Copy the code
Bytearray can be converted to bytes objects, but not very efficiently.
Converting # bytearray to bytes will generate a new object
>>> bn = bytes(ba)
>>> id(bn)
4604114344
>>> id(ba)
4552473544
Copy the code
0x06 Each type converts to each other
tuple->list
>>> tuple(l)
('a'.'b'.'c')
Copy the code
list->tuple
>>> t
('a'.'b'.'c')
>>> list(t)
['a'.'b'.'c']
Copy the code
str->list
>>> l = list('abc')
>>> l
['a'.'b'.'c']
Copy the code
list->str
>>> l
['a'.'b'.'c'] > > >' '.join(l)
'abc'
Copy the code
str->bytes
>>> s = '123'
>>> bytes(s)
TypeError: string argument without an encoding
>>> bytes(s,encoding='utf-8')
b'123'
Or use the encode() method of STR
>>> s.encode()
b'123'
Copy the code
bytes->str
>>> b = b'124'
>>> b
b'124'
>>> type(b)
<class 'bytes'>
>>> str(b,encoding='utf-8')
'124'
# or decode() using bytes
>>> b.decode()
'124'
Copy the code
0 x07 summary
These data types are native to Python, and you should choose the appropriate data type based on your actual requirements. For example, when you want to store multiple element types, use a list or tuple. Array, on the other hand, has relatively good spatial performance, but it can only store a single type.
I believe there are many business scenarios where a list or tuple will do the job, but other data structures need to be understood as well, whether we’re doing some basic components, thinking about the performance of the data structure, or reading someone else’s code.
0x08 Learning Materials
- Docs.python.org/3.1/library…
- Docs.python.org/zh-cn/3/lib…
- Docs.python.org/3/library/s…