python一些小的應用
阿新 • • 發佈:2019-02-02
#coding:utf-8 #利用dict特性去掉一個list中重複的資料 def del_repeate_data(): mylist = [1,1,2,3,4,5,4,2,5,5,5] d = {} for x in mylist: d[x] = 1 mylist = list(d.keys()) print mylist # del_repeate_data() #生成字典 def makedir(**kwargs): return kwargs # dic = makedir(red=1,green=2,blue=3) # print dic #字串的倒序排列 mystr = "abcdefghijk" print mystr[::-1] #陣列的倒序排列 myarr = [1,2,3,4,45,5,6] newarr = [] for i in reversed(myarr): newarr.append(i) # print newarr #遞迴 '''1+2+3+...+n''' def rec(n): if n>0: return n+rec(n-1) else:# n<=0: return 0 print rec(100) #如果在使用遞迴的時候,出現條件判斷,一定要把所有可能出現的條件都進行相應的處理 #求階乘 def fac(n): if n>0: return n*fac(n-1) elif n==0 or n==1: return 1 else: pass print fac(-4)