In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What are the application scenarios of the Python list? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Today, let's talk about the application scenario of the list in actual development.
In development, the choice of data structure is determined by the data characteristics and business scenarios we face.
Is the data individual or batch, small-scale or massive?
Are the data independent or interrelated?
Is the data generated randomly or sequentially?
What is the purpose of the data? Do you read and write frequently? Read-only or modify more?
Is the data applied to a multithreaded environment?
. (n multiple situations are omitted here.)
Once the data characteristics and business scenarios are identified, we can choose the appropriate tools from the development toolbox.
For list, first of all, it is a collection of objects, and you can use list when dealing with bulk data.
> alist = [i for i inrange (21)] > > alist > alist > alist.append (2021) > alist [0Me 1, 2, 4, 5, 6, 8, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20] > alist.append (2021) > alist]
We know that tuple can also be used to store multiple objects, but tuple is immutable, and once initialized, you can no longer increase or subtract the number of elements in it. Tuple syntactically guarantees that the number of elements will not be modified.
If you just don't want others to add or delete elements to the dataset, you should use tuple rather than list first. Conversely, if you need to dynamically adjust the number of elements in the data set, you should choose list.
Does this mean that all dynamic datasets can use list? No.
If your data is rarely modified and is read most of the time, it is ideal to use list for storage. Because list provides indexing and slicing operations, you can quickly access the elements.
> > alist [0, alist [10] 10 > alist [3:9] [3, 4, 5, 8] > > alist [3:9:2] [3] > >
If you just use append () to append elements to the tail of list, or delete the tail element of list, you can also rest assured to use list. Because it's very fast to add and remove elements at the end of the list.
> stack = [3 beats 4 beats 5] > stack.append (6) > stack.append (7) > stack [3 pencils 4 bees 5 pencils 5] > > stack.pop () 7 > > stack [3 pencils 4 pencils 5] > > stack.pop () 6 > > stack [3 pencils 4 pencils 5]
In this case, list is actually used as a stack.
However, if your program needs to frequently insert or delete elements in or in the middle of list, list is not quite suitable for your needs.
Because, the bottom layer of list is implemented through variable-length arrays. To insert or delete elements in the head or middle of an array, you need to move each element after the insertion position one by one. This will consume a lot of time when the amount of data is large, and it is inefficient.
In common business scenarios, frequent additions and deletions of intermediate elements are common in linked storage structures (such as linked lists), but not in linear storage structures (such as arrays).
If you want to use chained structures in Python, you can use collections.deque. Strictly speaking, collections.deque is not a complete chained structure, it is a chained structure with block data. Each block is a linear array.
Let's look at a scenario where operations need to be performed frequently at the beginning and end of a dataset: queue.
A queue is a first-in, first-out (FIFO) data structure in which data is inserted from the tail and taken out from the head. It's like we stand in line every day.
List is also not suitable for use as a queue because of the frequent deletion of header elements. It is inappropriate for many beginners to use list as a queue only from the literal meaning of the word list.
We can use collections.deque to implement queue operations.
> from collections import deque > queue = deque (["Eric", "John", "Michael"]) > queue.append ("Terry") > queue.append ("Graham") > queue deque (['Eric','John','Michael','Terry','Graham']) > queue.popleft ()' Eric' > queue.popleft () 'John' > > queue deque ([' Michael','Terry','Graham']])
List is suitable for dealing with dynamic datasets, especially for scenarios where there are far more read operations than write operations.
List can be used to implement stack operations.
List is not suitable to be used as a queue, but collections.deque can be used to implement queue operations.
After reading the above, have you mastered the application scenarios of the Python list? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.