day15 stack queue,遍历目录

模拟栈结构
stack = [ ]
压栈(向栈里存数据)

stack.append('a')
print(stack)
stack.apped('b')
print(stack)
stack.append('c')
print(stack)

打印结果:
['a']
['a', 'b']
['a', 'b', 'c']

出栈(向栈里取数据)

res1 = stack.pop()
print('res1=',res1)
print(stack)
res2 = stack.pop()
print('res2=',res2)
print(stack)
res3 = stack.pop()
print('res3=',res3)
print(stack)

打印结果:
res1= c
['a', 'b']
res2= b
['a']
res3= a
[]

队列--先进先出
创建一个队列

import collections
queue = collections.deque()
print(queue)

进队(存数据)

queue.append('a')
print(queue)
queue.append('b')
print(queue)
queue.append('c')
print(queue)

打印结果:
deque(['a'])
deque(['a', 'b'])
deque(['a', 'b', 'c'])

出队(取出数据)

ser1 = queue.popleft()
print('ser1=',ser1)
print(queue)
ser2 = queue.popleft()
print('ser2=',ser2)
print(queue)
ser3 = queue.popleft()
print('ser3=',ser3)
print(queue)

打印结果:
ser1= a
deque(['b', 'c'])
ser2= b
deque(['c'])
ser3= c
deque([])

递归遍历目录:

import os
def getAllDir(path):
    获取当前路径下所有文件
    files_list = os.listdir(path)
    处理文件是否目录
    for file_name in files_list:
          判断路径(绝对路径)
          file_abspath = os.path.join(path,file_name)
          if os.path.isdir(file_abspath):
              print('目录:' + file_name)
              getAllDir(file_abspath)
          else:
              print('普通目录:'+ file_name) 
getAllDir(r'D:\Python学习进度')
思路:调用函数,遍历路径下所有目录文件   都打印出来

栈模拟递归遍历目录(深度遍历)

def getAllDirDE(path):
    stack = [ ]     #  空栈
    stack.append(path)   #  压栈  (存数据)
  #   处理栈,当栈为空的时候结束循环
    while len(stack) != 0:
          dirpath = stack.pop()  #  出栈  (取出数据)
         # 获取当前目录下所有目录文件
          file_list = os.listdir(dirpath)
          for file_name in files_list:
              判断路径(合拼路径)
              file_abspath = os.path.join(path.file_name)
               if os.path.isdir(file_abspath):
                      print('目录:'+file_name)
                      stack.append(file_abspath)
               else:
                      print('普通文件:'+file_name)
getAllDirDE(r'D:\Python学习进度')

队列模拟递归遍历目录(广度遍历)

import os
import collections
def getAllDirQUE(path):
      #   创建一个队列
       queue = collections.deque() 
        queue.append(path)   #  进队(存数据)
        处理队列,当队列为空的是否就结束循环
        while len(queue) != 0:
                dirpath = queue.popleft()   #  出队(取数据)
                #  获取路径下的所有目录  
                 files_list = os.listdir(dirpath)
                  for file_name in file_list:
                    # 判断路径(绝对路劲,合拼路径)
                      file_abspath = os.path.join(path,file_name)
                      if os.path.isdir(file_abspath):
                            print('目录:'+file_name)
                            queue.append(file_abspath)
                      else:  
                             print('普通目录:'+file_name)
        
getAllDirQUE(r'D:\Python学习进度')
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 基础知识 1.1、 基本概念、 功能 冯诺伊曼体系结构1、计算机处理的数据和指令一律用二进制数表示2、顺序执...
    yunpiao阅读 10,858评论 1 22
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,801评论 1 32
  • 目录 1. 栈和队列1.用两个队列实现栈2.用两个栈实现队列3.实现一个栈,可以用常数级时间找出栈中的最小值4.判...
    MigrationUK阅读 8,155评论 4 20
  • 1)这本书为什么值得看: Python语言描述,如果学的Python用这本书学数据结构更合适 2016年出版,内容...
    孙怀阔阅读 14,328评论 0 15
  • 一个习惯站在潮头的,怎么习惯被人忽视,你可以说这是虚荣,但阿尔帕西诺曾经说过一句台词:“虚荣,我最爱的原罪。”
    HeyCoco阅读 728评论 0 0