1.在函數中調用本身python
2.超過遞歸的最大深度報錯,遞歸的最大深度:998大概git
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))
作字符串匹配的一種規則app
字符組:[],在同一個位置上可能出現的字符組成一個字符組 正則規則: [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:轉義
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)
python中的擴展數據類型dom
namedtuple 可命名元組
from collections import namedtuple Point = namedtuple('point',['x','y']) p = Point(1,2) print(p.x,p.y)
#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 比較佔內存 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模塊 隨機數模塊 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,'23',[1,2]])#隨機選擇一個返回 1或者’23‘或者[1,2] random.sample([1,2,3,4],2)#任意兩個元素 random.shuffle([1,2,3,4,5,6])#打亂次序