Python algorithm Tutorial Chapter I knowledge Points: The essence of List is explained by the example of inserting elements

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.