Python summary (primary data structure list and array)

1. Lists and tuples

         The list is dynamic and its length is not fixed. You can add, delete or change elements (mutable) at will.

         Tuples are static, with fixed length and size, and cannot be added, deleted or immutable.

        

tup = (1, 2, 3, 4)
new_tup = tup + (5, ) # Create a new tuple new_tup, and fill in the values of the original tuple in turn
new _tup
(1, 2, 3, 4, 5)

l = [1, 2, 3, 4]
l.append(5) # Add element 5 to the end of the original list
l
[1, 2, 3, 4, 5]

Lists and tuples in Python support negative indexes, - 1 for the last element, - 2 for the penultimate element, and so on.

l = [1, 2, 3, 4]
l[-1]
4

tup = (1, 2, 3, 4)
tup[-1]
4

In addition to basic initialization and indexing, lists and tuples support slicing operations:

l = [1, 2, 3, 4]
l[1:3] # Returns a sublist with indexes from 1 to 2 in the list
[2, 3]

tup = (1, 2, 3, 4)
tup[1:3] # Returns a child tuple with an index from 1 to 2 in a tuple
(2, 3) 

In addition, lists and tuples can be nested at will:

l = [[1, 2, 3], [4, 5]] # Each element of the list is also a list

tup = ((1, 2, 3), (4, 5, 6)) # Each element of a tuple is also a tuple

Of course, the two can also be converted to each other through the list() and tuple functions:

list((1, 2, 3))
[1, 2, 3]

tuple([1, 2, 3])
(1, 2, 3)

Finally, let's look at some built-in functions commonly used in lists and tuples:

l = [3, 2, 3, 7, 8, 1]
l.count(3) 
2
l.index(7)
3
l.reverse()
l
[1, 8, 7, 3, 2, 3]
l.sort()
l
[1, 2, 3, 3, 7, 8]

tup = (3, 2, 3, 7, 8, 1)
tup.count(3)
2
tup.index(7)
3
list(reversed(tup))
[1, 8, 7, 3, 2, 3]
sorted(tup)
[1, 2, 3, 3, 7, 8]

count(item) indicates the number of occurrences of item in the statistical list / tuple.

index(item) indicates the index of the first occurrence of the returned list / tuple item.

list.reverse() and list.sort() represent in-situ inversion of the list and sorting, respectively (note that tuples do not have these two built-in functions).

reversed() and sorted() also indicate that the list / tuple is inverted and sorted, and reversed() returns an iterator after conversion (the above example uses the list() function to convert it into a list); sorted() returns a new ordered list.

Differences in the storage of lists and tuples

As mentioned earlier, the most important difference between list and tuple is that list is dynamic and variable, while tuple is static and immutable. Such a difference is bound to affect the storage mode of the two.

l = [1, 2, 3]
l.__sizeof__()
64
tup = (1, 2, 3)
tup.__sizeof__()
48

Lists and tuples place the same elements, but the storage space of tuples is 16 bytes less than that of lists.

In fact, the list is dynamic and needs to store pointers to the corresponding elements (8 bytes for int type in the above example). In addition, the list is variable and requires additional storage and allocated length (8 bytes), so that the use of list space can be tracked in real time. When the space is insufficient, additional space can be allocated in time.

l = []
l.__sizeof__() // The storage space of the empty list is 40 bytes
40
l.append(1)
l.__sizeof__() 
72 // After adding element 1, the list allocates space for 4 elements (72 - 40) / 8 = 4
l.append(2) 
l.__sizeof__()
72 // Since space has been allocated before, element 2 is added and the list space remains unchanged
l.append(3)
l.__sizeof__() 
72 // ditto
l.append(4)
l.__sizeof__() 
72 // ditto
l.append(5)
l.__sizeof__() 
104 // After adding element 5, there is not enough space in the list, so an additional space for storing 4 elements is allocated

The above example roughly describes the process of list space allocation. In order to reduce the overhead of space allocation during each add / delete operation, Python allocates more space each time. This mechanism ensures the efficiency of its operation: the time complexity of add / delete is O (1).

But for tuples, the situation is different. The tuple length and size are fixed, and the elements are immutable, so the storage space is fixed.

After reading the previous analysis, you may feel that such a difference can be ignored. But imagine that if the number of list and tuple storage elements is one hundred million, one billion or more, can you ignore such differences?

Performance of lists and tuples

By studying the differences between the storage methods of lists and tuples, we can conclude that tuples are more lightweight than lists, so in general, the performance speed of tuples is slightly faster than that of lists.

In addition, Python will do some resource caching for static data in the background. Generally speaking, due to the existence of garbage collection mechanism, if some variables are not used, Python will recycle the memory occupied by them and return it to the operating system for use by other variables or other applications.

However, for some static variables, such as tuples, Python will temporarily cache this part of memory if it is not used and takes up little space. In this way, the next time we create tuples of the same size, Python can no longer send requests to the operating system to find memory, but can directly allocate the previously cached memory space, which can greatly speed up the running speed of the program.

The following example calculates the time required to initialize a list and tuple of the same element. We can see that the initialization speed of tuples is five times faster than that of lists.

python3 -m timeit 'x=(1,2,3,4,5,6)'
20000000 loops, best of 5: 9.97 nsec per loop
python3 -m timeit 'x=[1,2,3,4,5,6]'
5000000 loops, best of 5: 50.1 nsec per loop

However, if it is an index operation, the speed difference between the two is very small and almost negligible.

python3 -m timeit -s 'x=[1,2,3,4,5,6]' 'y=x[3]'
10000000 loops, best of 5: 22.2 nsec per loop
python3 -m timeit -s 'x=(1,2,3,4,5,6)' 'y=x[3]'
10000000 loops, best of 5: 21.9 nsec per loop

Of course, if you want to add, delete, or change elements, the list is obviously better. Tuples need to be created.

Usage scenarios for lists and tuples

So which one do lists and tuples use? According to the characteristics mentioned above, we will analyze the specific situation.

1. If the stored data and quantity remain unchanged, for example, if you have a function that needs to return the longitude and latitude of a location, and then directly transmit it to the front-end rendering, tuples are certainly more appropriate.

def get_location():
    ..... 
    return (longitude, latitude)

2. If the stored data or quantity is variable, for example, a log function of the social platform is to count which users' posts a user has seen in a week, then a list is more appropriate.

viewer_owner_id_list = [] # Each element records the IDs of all owner s that the viewer has seen in a week
records = queryDB(viewer_id) # Index the database and get the logs of a viewer within a week
for record in records:
    viewer_owner_id_list.append(record.id)

summary

Lists and tuples are ordered and can store collections of any data type. The main differences are the following two points.

The list is dynamic and variable in length. You can add, delete or change elements at will. The storage space of the list is slightly larger than the tuple, and the performance is slightly inferior to the tuple.

Tuples are static with fixed length and size. You cannot add, delete or change elements. Tuples are lighter than lists and have slightly better performance.

Thinking questions:

# Create an empty list
# option A
empty_list = list()

# option B
empty_list = []

The second of these two creation methods will be faster. The main difference is that list() is a function call. Python's function call creates a stack and performs a series of parameter checking operations. Compared with expensive, [] is a built-in C function that can be called directly, so it is efficient.

Tags: Python html5 SQL

Posted on Mon, 22 Nov 2021 12:29:46 -0500 by iamtheironman