Python Lesson 32-Stack

Stack: Meet the characteristics --> First in, last out, similar to the bullet clip in our lives

【note】

For the stack structure: Python does not encapsulate specific functions for it, we can use list (list) to simulate the characteristics of the stack

Use the list object to simulate the characteristics of the stack structure to access data: first in, last out

# Define a list object,stack(Variable name, reference name)
stack=[]

# Add data to the stack(Simulation push)
stack.append('A')print(stack)

stack.append('B')print(stack)

stack.append('C')print(stack)

# Pop data from the stack(Mock popup)
obj=stack.pop()print('pop up:'+obj)

obj=stack.pop()print('pop up:'+obj)

obj=stack.pop()print('pop up:'+obj)

Features of simulated stack structure to achieve deep traversal of directory traversal

import os
# Custom function: to achieve deep traversal of directory levels
def getAllFileST(path):
 # Define a list
 lt=[]
 # Will path(String, absolute path)Push
 lt.append(path)
 # Determine the number of loop executions according to the length of lt
 whilelen(lt)!=0:
  # Pop the data in lt onto the stack
  file_path=lt.pop()
  # Get file_Word content in path(Files, subdirectories)Return as a list
  file_list=os.listdir(file_path)
  # Loop processing file_list
  for file in file_list:
   # Restore each element to an absolute path value
   fileAbsPath=os.path.join(file_path,file)'''
   Is it a file or a directory?
   If it is a file, just print the file name
   If it is a directory, print the directory first and then push it on the stack
            '''
   if os.path.isfile(fileAbsPath):print('file:'+file)else:print('table of Contents:'+file)
    lt.append(fileAbsPath)

p=r'a.txt'getAllFileST(p)

Recommended Posts

Python Lesson 32-Stack
Python Lesson 32-Queue
Python Lesson 37-Module
Can Python implement the structure of the stack?
Python multithreading
Python CookBook
Python FAQ
Python3 dictionary
python (you-get)
Python string
Python basics
Python descriptor
Python basics 2
Python exec
Python notes
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python IO
Python multithreading
Python toolchain
Python3 list
Python multitasking-coroutine
Python overview
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4
Python basics 5