Statement: Because the translation of Chinese translations is too redundant, the useful knowledge points are listed.
directory of this document: First, using an example of inserting elements to explain the nature of list
</br>
first, the essence of list is explained by the example of inserting element
代码一:count = 10**5nums = []for i in range(count):nums.append(i)nums.reverse() #翻转功能
代码二:count = 10**5nums = []for i in range(count):nums.insert(0, i)
Analysis : The function of the two code is to fill the "99999,99998,...,0" number into the list nums, but its implementation in different ways. Wherein, the first code uses the loop, first adds the number to the end of the list nums, then uses the reverse function to flip, thus achieves the goal; the second code directly adds the number directly to the first end of the list nums using the Insert function. It looks like the second piece of code is more convenient, but in fact, the speed of the second code drops by two orders of magnitude.
</br>
principle : The traditional list (that is, linked lists) is implemented through a series of nodes, each node except the tail node has a pointer to the next node. The list in Python is not made up of nodes that point to each other, but rather a single contiguous chunk of memory, an array. In the case of traversal, the list is almost as efficient as an array; when directly accessed, the list needs to traverse from the beginning to find the element that needs to be accessed, while the array can be computed to get the target element in memory, and when it is inserted, the operation cost of the linked list is very low as long as the position of the inserted element is known Arrays, however, need to move all the elements to the right of the insertion point, which is much less efficient. This shows that the second paragraph of the code above each time inserting elements need to move all the elements that have been inserted, inefficient.
Python algorithm Tutorial Chapter I knowledge Points: The essence of List is explained by the example of inserting elements