千峰笔记-递归-栈与队列-目录遍历

 

 

递归调用:一个函数,调用了自身,称为递归调用

递归函数:一个会调用自身的函数

 

凡是循环能干的事,递归都能干

 

方式:

  1. 写出临界条件
  2. 找这一次和上一次的关系
  3. 假设当前函数已经能用,调用自身计算上一次的结果,再求出本次的结果

 

#求和

def sum1(n):

      sum = 0

      for x in range(1, n+1):

            sum += x

      return sum

 

res = sum1(5)

print(“res =”, res)

 

#递归

def sum2(n):

      if n == 1:

            return 1

      else:

             return n + sum2(n – 1)

 

栈与队列

 

 

栈:先进后出

#模拟栈结构

stack = []

#压栈(向栈里存数据)

stack.append(“A”)

print(stack)

stack.append(“B”)

stack.append(“C”)



#出栈(在栈里取数据)

res1 = stack.pop()  #取A

print(“res =”,res)

print(stack)



res 2= stack.pop()

print(“res =”,res)

print(stack)

 

队列

 

先进先出

import collections



#创建一个队列

queue = collections.deque()

print(queue)



#进队(存数据)

queue.append(“A”)

print(queue)

queue.append(“B”)

print(queue)

queue.append(“C”)

print(queue)



#出队(取数据)

res2 = queue.popleft()

print(“res1 =”,res1)

res2 = queue.popleft()

print(“res1 =”,res2)

res3 = queue.popleft()

print(“res1 =”,res3)

 

目录遍历

 

递归遍历

import os



def getAlldir(path):

      #得到当前目录下所有文件

      filesList = os.listdir(path)

#处理每一个文件

#sp += “           ”

for fileName in filesList:

      #判断是否是路径(用绝对路径)

      fileAbsPath = os.path.join(path, fileName)

      if os.path.isdir(fileAbsPath):

            print(“目录:”, fileName)

            #递归调用

            getAlldir(fileAbsPath)

      else:

            print(“目录:”, fileName)

print()



getAlldir(r” C:\Users\89460\Documents\PythonData”)

 

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

import os



def getAllDirDE(path):

      stack = []   #列表模拟

      stack.append(path)

     

      #处理栈,当栈为空时结束循环

      while len(stack) != 0:

            #从栈里取出数据

            dirPath = stack.pop()

            #print(dirPath)

            #目录下所有文件

            filesList = os.listdir(dirPath)

            #处理每一个文件,普通文件则打印,目录则压入栈

            for fileName in filesList:

                  fileAbsPath = os.path.join(path, fileName)

            if os.path.isdir(fileAbsPath):

                  #是目录就压栈

                  print(“目录:” + fileName)

                  stack.append(fileAbsPath)

            else:

                  #打印普通文件

                  print(“普通:” + fileName)

                 

getAllDirDE(r” C:\Users\89460\Documents\PythonData”)

 

队列模拟递归遍历(广度优先)

import os

import collections



def getAllDirQU(path):
      queue = collections.deque()

      #进队

      queue.append(path)

     

      while len(queue) != 0:

            #出队数据

            dirPath = queue.popleft()

            #找出所有文件

            filesList = os.listdir(dirPath)



            for fileName in filesList:

                  #绝对路径

                  fileAbsPath = os.path.join(dirPath, fileName)

                  #判断是否是目录,是则进队,不是就打印

                  if os.path.isdir(fileAbsPath):

                      print(“目录:” + fileName)

                      queue.append(fileAbsPath)

                  else:

                      #打印普通文件

                      print(“普通:” + fileName)





getAllDirQU(r” C:\Users\89460\Documents\PythonData”)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值