9.千峰教育递归与时间相关模块----自制随堂笔记

递归

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

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

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

sum2(0) + 1 = sum2(1)
sum2(1) + 2 = sum2(2)
sum2(2) + 3 = sum2(3)
sum2(3) + 4 = sum2(4)
sum2(4) + 5 = sum2(5)

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

def sum1(n):
	sum = 0
	for x in range(1, n + 1):
		sum += x
	return sum
res = sum1(5)
print("res =", res)

5 + sum2(4)
5 + 4 + sum2(3)
5 + 4 + 3 + sum2(2)
5 + 4 + 3 + 2 + sum2(1)
5 + 4 + 3 + 2 + 1

def sum2(n):
	if n == 1:
		return 1
	else:
		return n + sum2(n - 1)

res = sum2(5)
print("res =", res)

1.栈

特点:先进后出

# 模拟栈结构
stack = []
# 压栈(向站里存数据)
stack.append("A")
print(stack)
stack.append("B")
print(stack)
stack.append("C")
print(stack)

# 出栈(在栈里取数据)
res1 = stack.pop()
print("res1 = ", res1)
res2 = stack.pop()
print("res2 = ", res2)
res3 = stack.pop()
print("res3 = ", res3)
print(stack)

2.队列

特点:先进先出

import collections

# 创建一个对列
queue = collections.deque()
print(queue)

# 进队(存数据)
queue.append("A")
print(queue)
queue.append("B")
print(queue)
queue.append("C")
print(queue)

# 出队(取数据)
res1 = queue.popleft()
print("res1 =", res1)
print(queue)
res2 = queue.popleft()
print("res2 =", res2)
print(queue)
res3 = queue.popleft()
print("res3 =", res3)
print(queue)

1、递归遍历目录

import os
def getAllDirRE(path, sp = "  "):
	# 得到当前目录下所有的文件
	filesList = os.listdir(path)
	# 处理每一个文件
	sp += "  "
	for fileName in filesList:
		# 判断是否是路径(绝对路径)
		fileAbsPath = os.path.join(path, fileName)
		if os.path.isdir(fileAbsPath):
			print(sp + "目录:", fileName)
			getAllDirRE(fileAbsPath, sp)
		else:
			print(sp + "普通文件:", fileName)
getAllDirRE(r"D:\python教程")

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

import os
def getAllDirDE(path):
	stack = []
	stack.append(path)
	# 处理栈,当栈为空的时候结束循环
	while len(stack) != 0:
		# 从栈里取出数据
		dirPath = stack.pop()
		# 目录下所有文件
		filesList = os.listdir(dirPath)
		# 处理每一个文件,如果是普通文件则打印出来,如果是目录则将该目录的地址压栈
		
		for fileName in filesList:
			fileAbsPath = os.path.join(dirPath, fileName)
			if os.path.isdir(fileAbsPath):
				# 是目录就压栈
				print("目录:" + fileName)
				stack.append(fileAbsPath)
			else:
				# 打印普通文件
				print("普通:", fileName)


getAllDirDE(r"D:\python教程")

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

import os
import collections

def getAllDirQU(path):
	queue = collections.deque()
	# 进队
	queue.append(path)
	while len(queue) != 0:
		# 出队数据
		dirPath = queue.popleft()
		# 找出所有的文件
		fileList = os.listdir(dirPath)

		for fileName in fileList:
			# 绝对路径
			fileAbsPath = os.path.join(dirPath, fileName)
			# 判断是否是目录,是目录就进队,不是就打印
			if os.path.isdir(fileAbsPath):
				print("目录:" + fileName)
				queue.append(fileAbsPath)
			else:
				print("普通文件:" + fileName)

getAllDirQU(r"D:\python教程")

时间相关模块

UTC(世界协调时间):
格林尼治天文时间,世界标准时间,在中国来说是UTC-8
DST(夏令时):
是一种节约能源而人为规定的时间制度,在夏季调快1个小时

时间的表示形式

1、时间戳
以整形或浮点型表示时间的一个以秒为单位的时间间隔
这个时间间隔的基础值是从1970年1月1日凌晨开始算起
2、元组
一种python的数据结构表示,这个元组有9个整型内容
year
mouth
day
hours
minutes
seconds
weekday
julia day
flag (1 或 -1 或 0)
3、格式化字符串
%y 两位数的年份表示(00-99)

%Y 四位数的年份表示(000-9999)

%m 月份(01-12)

%d 月内中的一天(0-31)

%H 24小时制小时数(0-23)

%I 12小时制小时数(01-12)

%M 分钟数(00=59)

%S 秒(00-59)

%a 本地简化星期名称

%A 本地完整星期名称

%b 本地简化的月份名称

%B 本地完整的月份名称

%c 本地相应的日期表示和时间表示

%j 年内的一天(001-366)

%p 本地A.M.或P.M.的等价符

%U 一年中的星期数(00-53)星期天为星期的开始

%w 星期(0-6),星期天为星期的开始

%W 一年中的星期数(00-53)星期一为星期的开始

%x 本地相应的日期表示

%X 本地相应的时间表示

%Z 当前时区的名称

%% %号本身

import time
# 返回当前时间的时间戳
c = time.time()
print(c)

# 将时间戳作为UTC时间元组
t = time.gmtime(c)
print(t)

# 将时间戳转为本地时间元组
b = time.localtime(c)
print(b)

# 将本地时间元组转成时间戳
m = time.mktime(b)
print(m)

# 将时间元组转成子字符串
s = time.asctime(b)
print(s)

# 将时间戳转为字符串      time.asctime(time.localtime(time.time()))
p = time.ctime(c)
print(p)

# 将时间元组转换成给定格式的字符串,参数2为时间元组,如果没有参数2,默认转的是当前时间
q = time.strftime("%Y-%m-%d %X", b)
print(q)
print(type(q))

# 将时间字符串转为时间元组
w = time.strptime(q, "%Y-%m-%d %X")
print(w)

图片
延迟一个时间,整型或者浮点型

time.sleep(4)

返回当前程序的cpu执行时间
Unix系统始终返回全部的运行时间
windows从第二次开始,都是以第一个调用此函数的开始时间戳作为基数

import time
y1 = time.clock()
print("%d" % y1)
time.sleep(2)
y2 = time.clock()
print("%d" % y2)
time.sleep(2)
y3 = time.clock()
print("%d" % y3)

import time
time.clock()
sum = 0
for i in range(10):
	sum += 1
print(time.clock())

datatime比time高级了不少,可以理解为datatime基于time进行了
封装,提供了更为实用的函数,datatime模块的接口更直观,更容易
调用

模块中的类:
datetime 同时有时间和日期
timedelta 主要用于计算时间的跨度
tzinfo 时区相关
time 只关注时间
date 只关注日期

import datetime

# 获取当前时间
d1 = datetime.datetime.now()
print(d1)
print(type(d1))

# 指定的时间
d2 = datetime.datetime(1999, 10, 1, 10, 28, 25, 123456)
print(d2)

# 将时间转为字符串
d3 = d1.strftime("%Y-%m-%d %X")
print(d3)
print(type(d3))

# 将格式化字符转换为datetime对象
# 注意:转换的格式要与字符串一致
d4 = datetime.datetime.strptime(d3, "%Y-%m-%d %X")
print(d4)

d5 = datetime.datetime(1999, 10, 1, 10, 28, 25, 123456)
d6 = datetime.datetime.now()
d7 = d6 - d5
print(d7)
print(type(d7))
# 间隔的天数
print(d7.days)
# 间隔的天数除外的秒数
print(d7.seconds)

calends 日历模块

import calendar

# 使用
# 返回指定某年某月的日历

print(calendar.month(2017, 7))
# 返回指定某年的日历
print(calendar.calendar(2017))
# 闰年返回True,否则返回False
print(calendar.isleap(2000))
# 返回某个月的weekday的第一天和这个月的所有天数
print(calendar.monthrange(2017, 7))
# 返回某个月以每一周为元素的列表
print(calendar.monthcalendar(2017, 7))

分类一个装满邮箱的txt文件,按照169,126,qq,并且装入一个文件夹中,生成新的txt

# 队列模拟递归遍历目录(广度遍历)
import os
import collections
def work(path):
	resPath = r"D:\python教程\千峰教育400集python教程sublime\file1"
	# 打开文件
	with open(path, "r") as f:
		while True:
			# laphae11985@163.com---198587
			lineInfo = f.readline()
			if len(lineInfo) < 5:
				break
			# 邮箱的字符串
			# laphae11985@163.com
			mailStr = lineInfo.split("---")[0]
			# 邮箱类型的目录
			# D:\python教程\千峰教育400集python教程sublime\file1\163
			print(mailStr)
			fileType = mailStr.split("@")[1].split(".")[0]
			dirStr = os.path.join(resPath, fileType )
			if not os.path.exists(dirStr):
				# 不存在,创建
				os.mkdir(dirStr)
			filePath = os.path.join(dirStr, fileType + ".txt")
			with open(filePath, "a") as fw:
				fw.write(mailStr+"\n")
			# 创建文件
def getAllDirQU(path):
	queue = collections.deque()
	# 进队
	queue.append(path)
	while len(queue) != 0:
		# 出队数据
		dirPath = queue.popleft()
		# 找出所有的文件
		fileList = os.listdir(dirPath)
		for fileName in fileList:
			# 绝对路径
			fileAbsPath = os.path.join(dirPath, fileName)
			# 判断是否是目录,是目录就进队,不是就打印
			if os.path.isdir(fileAbsPath):
				print("目录:" + fileName)
				queue.append(fileAbsPath)
			else:
				# 处理普通文件
				work(fileAbsPath)
getAllDirQU(r"D:\python教程\千峰教育400集python教程sublime\file1")
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值