python视频笔记18(递归/栈/队列)

【递归】:

递归调用:

一个函数,调用了自身,这种调用叫做递归调用

递归函数:

一个会调用自身的函数

	def function():
		print ("123")
	def func():
		func()
凡是循环能干的事,递归都可以
方式:
		1:写出临界条件
		2:找这一次和上一次的关系
		3:假设当前函数已经能用,调用自身计算上一次的结果,在求出本地的结果
eg:

输入一个数字(大于等于1) 求1+2+3+4+···n的和

			def sun(n)
				sun = 0
				for x in range(1,n+1):
					sun += x
				return sun
			res = sun(5)
			print ("res = ", res)
递归函数实现:
				1+2+3+4+5
				sun(1)+0 = sun(1)
				sun(1)+2= sun(2)
				sun(2)+3= sun(3)
				sun(3)+4 = sun(4)
				sun(4)+5 = sun(5)
				def sun(n):
					if n == 1:
						return 1
					else:
						return n + sun(n-1)
					res = sun(5)
					print ("res = ", res)

【栈】:

先进后出

模拟栈结构:

		stack = []
压栈(像栈里面存数据)
			stack.append("a")
			print (stack)
			stack.append("b")
			stack.append("c")
			print (stack)
出栈(在栈里面取数据)
			res1 = stack.pop()
			print ("res1 = ",res1 )
			print (stack)
			res2 = stack.pop()
			print ("res2 = ",res2 )
			print (stack)
			res3 = stack.pop()
			print ("res3 = ",res3 )
			print (stack)

【队列】:

就像排队,先进先出

	import collections
#创建队列
	queue  = collections.deque()
	#进入队列(存数据)
	queue.append("a")
	print (queue)
	queue.append("b")
	queue.append("c")
	#出队(取数据)
	resl = queue.popleft()
	print("res1 = ",res1)
	res2 = queue.popleft()
	print ("res2 = ", res2)
	res3 = queue.popleft()
	print("res3 = ",res3)

【遍历目录】:

1:队列遍历目录
	import os
	def getAllDir(path,sp = " "):
		fileList = os.listdir(path)
	#得到当前目录下所有文件
	getAllDir(r"/usr/local/tmp")
	#处理每一个文件
	sp += "  "
	for fileName in fileList:
		#判断是否为路径(用绝对路径)
		fileAbsPath = os.path.join(path,fileName)
		if os.path isdir(fileAbsPath):
			print (sp + "dir", fileName)
			getAllDir(fileAbsPath,sp):
		else:
			print (sp +"file", fileName)

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

1:队列遍历目录
	import os
	def getAllDir(path,sp = " "):
		fileList = os.listdir(path)
	#得到当前目录下所有文件
	getAllDir(r"/usr/local/tmp")
	#处理每一个文件
	sp += "  "
	for fileName in fileList:
		#判断是否为路径(用绝对路径)
		fileAbsPath = os.path.join(path,fileName)
		if os.path isdir(fileAbsPath):
			print (sp + "dir", fileName)
			getAllDir(fileAbsPath,sp):
		else:
			print (sp +"file", fileName)
2:栈模拟递归遍历目录(深度遍历)
	import os 
	def getAllDirDe(path):
		stack = []
		stack.append(path)
		#处理栈,当栈为空的时候结束循环
		while len(stack) != 0:
			#从栈里取出数据
			dirPath = stack.pop()
			#目录下所有文件按
			fileList = os.listdir(dirPath)
				for fileName in fileList:
					fileAbsPath = os.path.join(dirPath,fileName)
					if os.path.isdir(fileAbsPath):
						#是目录就压栈
						print ("dir",fileName)
						stack.append(fileAbsPath)
					else:		
						print("file",fileName)
getAllDir(r"/usr/local/tmp")

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

	import os
	import collections
	def getAllDir(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("dir",fileName)
					queue.apppe(fileAbsPath)
				eles:
					print ("file",fileName)
	getAllDirQU(r"/ust/local/tmp")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值