In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the list essence of Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the list essence of Python".
First, use the example of inserting elements to explain the nature of list in detail.
Code 1: count = 10**5nums = [] for i in range (count): nums.append (I) nums.reverse () # Flip function Code 2: count = 10**5nums = [] for i in range (count): nums.insert (0, I)
Brief analysis: the function of the two pieces of code is to fill the number "999999998, …, 0" into the list nums, but its implementation is different. Among them, the first section of code uses a loop to add numbers to the end of the list nums, and then uses the reverse function to flip, thus achieving the purpose; the second section of code directly uses the insert function to add numbers directly to the first end of the list nums. It seems that the second piece of code is more convenient, but in fact, the speed of the second piece of code has slowed down by two orders of magnitude.
Principle: traditional lists (that is, linked lists) are implemented through a series of nodes, and each node except the tail node has a pointer to the next node. The list in Python is not made up of nodes pointing to each other, but a single contiguous block of memory, that is, an array. When traversing, the efficiency of the linked list is about the same as that of the array; for direct access, the linked list needs to traverse from scratch to find the elements that need to be accessed, while the array can be calculated to get the location of the target element in memory; when inserting, as long as you know the location of the inserted element, the operation cost of the linked list is very low, while the array needs to move all the elements to the right of the insertion point, which is much less efficient. Thus it can be seen that the second section of the above code needs to move all the elements that have already been inserted each time it inserts an element, which is inefficient.
Thank you for your reading, the above is the content of "what is the list essence of Python". After the study of this article, I believe you have a deeper understanding of what the list essence of Python is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.