In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "stack example analysis in Python". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "stack example analysis in Python"!
1. Problem description
Data types in Python include lists, tuples, dictionaries, queues, stacks, trees, and so on. Lists and tuples are built-in data structures in python; stacks and queues need to be defined by ourselves.
Stack is a data structure that is only allowed to be inserted and taken out at one end, this end is usually called the top of the stack, the other end is called the bottom of the stack, and the one with no data is called empty stack. Because this data type is defined by ourselves, there are many functions that need to be written by ourselves to implement. So let's take a look at the functions here.
2. Solution
The basic operations of the stack are: generating the stack, entering the stack, going out of the stack, returning the elements at the top of the stack, judging whether the stack is empty, and returning the number of elements in the stack.
First we need to create a stack:
Class stack (object): def _ _ init__ (self): self.__list = []
An empty stack is created here. If you want to detect it, you can also use a function to detect whether it is empty:
Def is_empty (self): return self.__list = = [] # return not self.__list
These are two return detection methods, either of which can be completed.
Next, you create the stack and know whether the detection stack is empty or not. As the definition says, because the stack is a data structure defined by ourselves and satisfies a last-in-first-out rule, the preservation and extraction of elements are naturally different. We call it going into and out of the stack. First of all, we just need to define a push function:
Def push (self,item): self.__list.append (item)
Then define a function pop to destack:
Def pop (self): self.__list.pop ()
The peek function returns the elements at the top of the stack:
Def peek (self): if self.__list: return self.__list [- 1] else: return None
The size function returns the number of elements in the stack:
Def size (self): return len (self.__list) so far, I believe you have a deeper understanding of "stack example analysis in Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.