Nonsense 1 List 2 strings stack 1 stack of data structure 2 stack application 21 parentheses order detection 22 before suffix expression 3 solution Maze problem Stack method queue 1 Queue data structure
1. Nonsense
In this section, we use the fastest speed to get through some of the more important data structures built into Python. 1.1 List
MyList = [1024, 3, True, 6.5]
mylist.append (False)
print (myList)
Mylist.insert (2,4.5)
print (myList)
print (Mylist.pop ()) print (
myList) print (
mylist.pop (1))
print (myList)
Mylist.pop (2)
Print (myList)
mylist.sort () print (
myList)
mylist.reverse ()
print (myList)
print ( Mylist.count (6.5))
print (Mylist.index (4.5))
Mylist.remove (6.5)
print (myList)
del mylist[0]
Print (myList)
[1024, 3, True, 6.5, False]
[1024, 3, 4.5, True, 6.5, False]
False
[1024, 3, 4.5, True, 6.5]
3
[1024, 4.5, True, 6.5]
[1024, 4.5, 6.5]
[4.5
, 6.5, 1024] [1024, 6.5, 4.5]
1
2
[1024, 4.5]
[4.5]
1.2 Strings
For more on string operations, you can refer to the Improve your Python code (9)
>>> myname
' David '
>>> myname.upper ()
' David '
>>> myname.center (10)
' David '
>>> myname.find (' V ')
2
>>> myname.split (' V ')
[' Da ', ' ID ']
2. Stack
2.1 Data structure of the stackStack () creates a new stack, is empty. It needs no parameters and returns an empty stack. Push (item) adds a new item to the top of the stack. It needs the item and returns nothing. Pop () removes the top item from the stack. It needs no parameters and returns the item. The stack is modified. Peek () returns the top item from the stack but does does not remove it. It needs no parameters. The stack is not modified. IsEmpty () tests to the whether of the stack is empty. It needs no parameters and returns a Boolean value. The size () returns the number of items on the stack. It needs no parameters and returns an integer.
Class Stack (object):
def __init__ (self):
self.items = []
def isempty (self): return
Self.items = = []
def push (Self,item):
self.items.append (item)
def pop (self): return
Self.items.pop ()
def Peek ( Self): return
self.items[-1]
def size (self): return
len (self.items)
As you can see, in Python, the stack is a data structure that can be implemented and replaced by a list. 2.2 Application of Stack 2.2.1. Bracket Sequence Detection
Class Stack (object):
def __init__ (self):
self.items = []
def isempty (self): return
Self.items = = []
def push (Self,item):
self.items.append (item)
def pop (self): return
Self.items.pop ()
def Peek ( Self): return
self.items[-1]
def size (self): return
len (self.items)
def parchecker (inputstring) :
s = Stack () in
inputstring: if letter
= = ' (':
s.push (letter)
elif letter = = ') ':
if S.isempty (): return
False
else:
s.pop () return
s.isempty ()
Print (Parchecker (()) )) Print (Parchecker (()) Print (Parchecker ("())"))
True
false
2.2.2 Before/middle/suffix expressions
What is the front/middle/suffix expression, no longer repeat here.
infix expression---> prefix expression
def infixtopostfix (infixexpr): Prec = {} prec["*" = 3 prec["/"] = 3 prec["+"] = 2 prec["-"] = 2 p
Rec["("] = 1 Opstack = Stack () postfixlist = [] tokenlist = Infixexpr.split () for token in tokenlist: If token in ' abcdefghijklmnopqrstuvwxyz ' or token in ' 0123456789 ': postfixlist.append (token) elif
token = = ' (': Opstack.push (token) elif token = = ') ': Toptoken = Opstack.pop () While Toptoken!= ' (': Postfixlist.append (toptoken) Toptoken = Opstack.pop () Else
: while (not Opstack.isempty ()) and \ (Prec[opstack.peek ()] >= Prec[token]): Postfixlist.append (Opstack.pop ()) Opstack.push (token) while not Opstack.isempty (): postfixlist . Append (Opstack.pop ()) return "". Join (postfixlist) print (Infixtopostfix ("A * B + C * D")) Print (Infixtopostfix ("a + B) * C-(D-E) *(F + G) ")")
Expression--> suffix expression
def postfixeval (postfixexpr):
operandstack = Stack ()
tokenlist = Postfixexpr.split () for
token in Tokenlist:
If token in "0123456789":
operandstack.push (int (token))
else:
operand2 = Operandstack.pop ()
operand1 = Operandstack.pop () result
= Domath (token,operand1,operand2)
Operandstack.push (Result) return
Operandstack.pop ()
def domath (OP, OP1, OP2):
if op = = "*":
return OP1 * op2
elif op = = "/": Return
op1/op2
elif op = "+": return
OP1 + op2
else:
ret Urn op1-op2
print (Postfixeval (' 7 8 + 3 2 +/'))
2.3 Solving Maze Problem: Stack method
I'm tired, I don't write 3. data structure of queue 3.1 queues
What is a queue. This is the queue:
The target audience for this article is not kindergarten children.
Structure of queues:
1. Queue () Creates a new queue, is empty. It needs no parameters and returns an empty queue.
2. Enqueue (item) adds a new item to the rear of the queue. It needs the item and returns nothing.
3. Dequeue () Removes the front item from the queue. It needs no parameters and returns the item. The queue is modified.
4. IsEmpty () tests to, whether the queue is empty. It needs no parameters and returns a Boolean value.
5. Size () returns the number of items in the queue. It needs no parameters and returns an integer.
Class Queue (object):
def __init__ (self):
self.items = []
def isempty (self): return
Self.items = = []
def enqueue (self, item):
Self.items.insert (0,item)
def dequeue (self): return
Self.items.pop ()
def size (self): return
len (self.items)
In fact, in Python3, you can import a third party queue library through the import queue.