字典查询键值
for k in d.keys(): print(k) for v in d.values(): print(v) for a,b in d.items(): print(a,b)
递归
def Number(at): if at==0: return 0 elif at==1: return 1 else: return Number(at-2)+Number(at-1) num=input("请输入:") print(Number(int(num)))
快排排序
def Kuaipai(alist): if len(alist)<2: return alist tom=alist[0] left=[x for x in alist[1:] if x<=tom] right=[i for i in alist[1:] if i>tom] return Kuaipai(left)+[tom]+Kuaipai(right) li=[1,5,7,9,3,8,6] print(Kuaipai(li))
冒泡排序
def Maopao(alist): for i in range(len(alist)-1): for j in range(len(alist)-i-1): if alist[j]>alist[j+1]: alist[j],alist[j+1]=alist[j+1],alist[j] li=[1,5,7,9,3,6,4] Maopao(li) print(li)
二分查找
def Erfen(alist,a): num=0 high=len(alist)-1 while num<=high: mid=(num+high)//2 if alist[mid]==a: return mid elif alist[mid]<a: num=mid+1 else: high=mid-1 return False alista=[1,2,3,4,5,6,7,8] aa=6 print(Erfen(alista,aa))