递归,re,time,random

时间:2021-08-23 22:03:20

递归函数

1.在函数中调用自己

2.超过递归的最大深度报错,递归的最大深度:998大概

3.递归的缺点:占内存

4.优点:代码简单

import sys
sys.setrecursionlimit(2000)#修改最大深度,#不要随意修改

实现斐波那契数列

#斐波那契数列 1,1,2,3,5,8  后一项为前两项的和
def fib(n):
if n==1 or n==2:
res=1
else:
res = fib(n-1)+fib(n-2)
return res
print(fib(6)) #循环实现求斐波那契数列
def fib(n):
a,b = 1,1
for i in range(0,n):
a,b = b,a+b
print(a)
fib(5)

递归实现二分查找算法

二分查找算法
def fin(li,searth,start=0,end=None):
end = len(li) if end is None else end
min = (end-start)//2+start
if start<=end:
if start == end and searth != li[start - 1]:
print('没有找到该数据,可能不在列表中')
elif searth<li[min]:
fin(li,searth,start=start,end=min-1)
elif searth>li[min]:
fin(li,searth,start=min+1,end=end)
else:print(min)
else:
print('没有找到该数据,可能不在列表中')
fin([1,2,3,4,5,6,7,8,9],80)

递归实现阶乘

def factorial(n):
if n==1:
res = 1
else:
res = n*factorial(n-1)
return res
print(factorial(3))

re模块

正则表达式

做字符串匹配的一种规则

正则规则

字符组:[],在同一个位置上可能出现的字符组成一个字符组
正则规则:
[0-9]:匹配数字
[a-z]:匹配小写字母
[A-Z]:匹配大写字母
.:匹配除换行符以外的任意字符
\w:匹配数字字母下划线(word)
\s:匹配任意的空白符(space)
\d:匹配任意数字(digit)
\W:匹配非数字字母下划线
\S:匹配任意非空白符(space)
\D:匹配任意非数字(digit)
\n:匹配换行符
\t:匹配制表符
\b:匹配一个单词的结尾
^a:匹配字符串的开头,以a开头
$a:以a结尾
a|b:匹配a或b
匹配次数(默认贪婪匹配,尽可能多的匹配)
*:匹配多次或0次
+:匹配多次
?:匹配0次或者1次,加在量词后面表示非贪婪匹配(尽可能少的匹配)
{n}:匹配n次,必须是n次
{n,}:匹配n次或更多
{n,m}:匹配n到m次
分组():对多个字符整体进行统一的约束
r'\d':表示不对字符串转义
.*?:一个也不匹配
[^]:除了字符组里面的都匹配
\\w:转义

re模块的常用方法

findall,search,match
import re
res = re.findall('[j]','jcc jcc')#返回所有匹配的元素放在列表中
print(res)#分组优先

res2 = re.search('c','jcc jcc')#从前往后匹配,找到一个符合条件的就返回,返回的变量需要调用group方法才能得到,如果没有匹配的内容,返回None
print(res2.group())
取分组中的内容 import re
name = 'name'
res = re.search('\d(?P<name>\D+)+','iwuedh13245isdbc')
print(res.group(name))
print(res.group(1))
res3 = re.match('[ab]','b')#从头开始匹配,如果从头开始可以匹配上,就返回一个变量,调用group方法获取
print(res3.group()) # split:根据正则表达式来分割
res4 = re.split('[ab]','abcdhyt')
# sub:根据规则替换
res5 = re.sub('\d','a','uyahsgdbaaa',1)

collections模块

python中的扩展数据类型

namedtuple

namedtuple 可命名元组
from collections import namedtuple
Point = namedtuple('point',['x','y'])
p = Point(1,2)
print(p.x,p.y)

deque

#deque 队列 先进先出
import queue
q = queue.Queue()
q.put(10)
print(q.get())
# get的时候队列中如果没有值,则该程序会阻塞
print(q.qsize())#判断队列中是否有值 #deque 双管队列,可以从两端放数据和取数据,可以插队
from collections import deque
q = deque([1,2])
q.append(3)#从后面放数据
q.appendleft(4)#从前面放数据
q.pop()#从后面取数据
q.popleft()#从前面取数据

orderdDict

#有序字典 orderdDict  比较占内存
from collections import OrderedDict
od = OrderedDict([('a',1),('b',2)])
# key是有序的 # defaultDict 字典中的每一个key值都有默认值
from collections import defaultdict
my_dict = defaultdict(list(1))

时间模块

时间的三种表示形式

# 时间的表示方式

# 1.时间戳(timestamp)返回的是float类型:计算机识别
time.time() # 2.格式化时间(format string):人看
time.strftime('%Y-%m-%d %H:%M:%S') # 3.结构化时间(struct_time):做计算
year = time.localtime().tm_year

形式之间的转换

# 时间戳-->结构化
time.time()
time.localtime()
time.gmtime()
# 结构化-->时间戳
time.mktime(time.localtime())
# 格式化-->结构化
time.strptime('2012-12-11','%Y-%m-%d')
# 结构化-->格式化
time.strftime('%Y-%m-%d',time.localtime(200000)) time.asctime()#显示的很全面
import time
time.sleep(1)#让程序在此处停留
time.time()#返回时间

random模块

# random模块 随机数模块
import random
random.random()#返回一个大于0小于1的小数
random.uniform(1,3)#返回一个大于1小于3的小数
random.randint(1,5)#返回随机1到5,包含5之间的整数
random.randrange(1,5,2)#随机返回1-10之间的整数,不包括10,可指定步长
random.choice([1,'',[1,2]])#随机选择一个返回 1或者’23‘或者[1,2]
random.sample([1,2,3,4],2)#任意两个元素
random.shuffle([1,2,3,4,5,6])#打乱次序