要求:給定一個列表 [13,22,6,99,11] 通過程序實現讓他們可以從小到大排序。
思路:讓相鄰的兩個數互比大小,存在大數就放在右邊。
我的實現:
#!/usr/bin/env python# -*- coding: utf-8 -*-__author__ = 'lilon'li = [33,11,88,77,44,345,223,113,555,23,3]PRint liprint "**************************8"#定義冒泡函數,計算排序,最大數放在右面def maopao(): #循環,從1開始,例如range(1,5) 1,2,3,4 注意沒有5 for n in range (1,len(li)): #循環,從0開始到 例如range(5-1 4) range(5-2 3) range(5-3 2) range(5-4 1) for m in range(len(li)-n): #給num1賦值 li[m] num1 = li[m] #給num2賦值 li[m+1] num2 = li[m+1] #比較num1 num2 if num1 > num2: #通過臨時變量temp交換li[m] 和 li[m+1] temp = num1 li[m] = num2 li[m+1] = temp print li print li pass#定義一個主函數def main(): maopao()#限制程序入口if __name__ =="__main__": main()
默認配置
global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 infodefaults log global mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms option dontlognulllisten stats :8888 stats enable stats uri /admin stats auth admin:1234frontend oldboy.org bind 0.0.0.0:80 option httplog option httpclose option forwardfor log global acl www hdr_reg(host) -i www.oldboy.org use_backend www.oldboy.org if wwwbackend www.oldboy.org server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000原配置文件
修改需求
1、查 輸入:www.oldboy.org 獲取當前backend下的所有記錄2、新建 輸入: arg = { 'bakend': 'www.oldboy.org', 'record':{ 'server': '100.1.7.9', 'weight': 20, 'maxconn': 30 } }3、刪除 輸入: arg = { 'bakend': 'www.oldboy.org', 'record':{ 'server': '100.1.7.9', 'weight': 20, 'maxconn': 30 } }需求
#!/usr/bin/env python# -*- coding: utf-8 -*-__author__ = 'lilon'import osimport json#定義好一個函數,用來做查找def fetch(ha): backendlist = ha #先準備一個列表,在后面用來加入查到的backend字段下的server信息 serverinfo = [] # print "backend %s" %backendlist #使用with的方式打開文件,可以自動的關閉文件,減少我們出錯誤的可能 with open ("oldboy.conf","r")as obj: """ #靈活添加flag,使用flag做一個開關,匹配到了相關的backend就把flag設置為True, 直到遇到了backend開頭的其他的字段,就把flag設置為False, 中間的就是我們需要的信息,使用列表的append()方法就可以添加了 """ flag = False #循環,一次一行,處理大文件必須的方式 for line in obj: if line.strip() == "backend %s" % backendlist: flag = True continue #靈活性代碼 startswitch方法 匹配開頭是backend的行 if flag and line.strip().startswith("backend"): flag = False break """ #當我們確認了flag是True, 意味著上面已經找到了相關的backend, 而且把flag設置為了True了, 所以我們現在可以通過append()方法來把相關的server info 加入到了列表里面 """ if flag and line.strip(): serverinfo.append(line.strip()) if serverinfo: return serverinfo#定義一個函數,做增加def backendadd(**dict_info): fetch_list = [] backend_title = dict_info.get("backend") current_record_pre = "server {server} weight {weight} maxconn {maxconn}" current_record = current_record_pre.format(**dict_info.get("record")) fetch_list = fetch(backend_title) if fetch_list: if current_record in fetch_list: pass else: fetch_list.append(current_record) with open("oldboy.conf","r") as objConf, open("conf.temp","w") as objTemp: flag = False isWrite = True for line in objConf: if line.strip() == "backend %s" %backend_title: objTemp.write(line) flag = True continue if flag == True and line.strip().startswith("backend"): flag = False if flag == True: if isWrite: for new_line in fetch_list: temp = "%s%s/n" %(" "*8,new_line) objTemp.write(temp) isWrite = True else: objTemp.write(line) if os.path.isfile("oldboy.bak"): os.remove("oldboy.bak") os.rename("oldboy.conf","oldboy.bak") os.rename("conf.temp","oldboy.conf") return True else: with open("oldboy.conf","r") as objConf, open("conf.temp","w") as objTemp: for line in objConf: objTemp.write(line) objTemp.write("backend %s/n" %backend_title) objTemp.write("%s%s/n" %(" "*8,current_record)) if os.path.isfile("oldboy.bak"): os.remove("oldboy.bak") os.rename("oldboy.conf","oldboy.bak") os.rename("conf.temp","oldboy.conf") return Truedef delitem(**dict_info): fetch_list = [] backend_title = dict_info.get("backend") current_record_pre = "server {server} weight {weight} maxconn {maxconn}" current_record = current_record_pre.format(**dict_info.get("record")) fetch_list = fetch(backend_title) if fetch_list: if current_record in fetch_list: with open("oldboy.conf","r") as objConf, open("conf.temp","w") as objTemp: if len(fetch_list) > 1: for line in objConf: if line.strip() == current_record: continue else: objTemp.write(line) else: for line in objConf: if line.strip() == "backend %s" %backend_title: continue if line.strip() == current_record: continue else: objTemp.write(line) if os.path.isfile("oldboy.bak"): os.remove("oldboy.bak") os.rename("oldboy.conf","oldboy.bak") os.rename("conf.temp","oldboy.conf") return True else: pass else: pass passdef backup(): os.remove("oldboy.conf") os.rename("oldboy.bak","oldboy.conf")#定義一個主函數def main(): ha = "buy.oldboy.org" dictItem = { 'backend': 'buy.oldboy.org', 'record':{ 'server': '101.1.71.90 100.1.7.90', 'weight': 22, 'maxconn': 3000 } } userInput = int(raw_input("hi plese input your choice:/n 1.search /n 2.add /n 3.del /n")) if userInput == 1: print "you select search." # userInputSearch = raw_input("input a backend url") searchEnd = fetch(ha) if searchEnd: for line in searchEnd: print line print "search End!" else: print "sorry not found!" elif userInput == 2: print "you select add." addEnd = backendadd(**dictItem) if addEnd: print "add successful" else: print "error!!!" elif userInput == 3: print "you select del." delEnd = delitem(**dictItem) if delEnd: print "del successful" else: print "no file in their" if os.path.isfile("oldboy.temp.conf"): os.remove("oldboy.temp.conf") elif userInput == 4: print "this is a super command" backup() else: print "sorry, i can not do that!!!"#限制程序入口if __name__ =="__main__": main()
學習條件運算時,對于簡單的 if else 語句,可以使用三元運算來表示,即:
# 普通條件語句if 1 == 1: name = 'wupeiqi'else: name = 'alex' # 三元運算name = 'wupeiqi' if 1 == 1 else 'alex'
#!/usr/bin/env python# -*- coding: utf-8 -*-__author__ = 'EchoRep'def func1(arg): return arg + 1func2 = lambda a: a+1func3 = lambda a,b: a+bdef lambdax(): result = func1(100) print result result2 = func2(1000) print result2 result3 = func3(100,211) print result3#定義一個主函數def main(): lambdax() pass#限制程序入口if __name__ == "__main__": main()
遍歷序列,對序列中每個元素進行操作,最終獲取新的序列

li = [11, 22, 33]new_list = map(lambda a: a + 100, li)
li = [11, 22, 33]sl = [1, 2, 3]new_list = map(lambda a, b: a + b, li, sl)
#!/usr/bin/env python# -*- coding: utf-8 -*-__author__ = 'EchoRep'def func1(args): return args ** 128#定義一個主函數def main(): li =[2,3,4] l1 = [1,2,3] l2 = [2,3,4] l3 = [3,4,5] print map(lambda a,b,c:a+b+c,l1,l2,l3) pass#限制程序入口if __name__ == "__main__": main()
對于序列中的元素進行篩選,最終獲取符合條件的序列

li = [11, 22, 33]new_list = filter(lambda arg: arg > 22, li)#filter第一個參數為空,將獲取原來序列
#!/usr/bin/env python# -*- coding: utf-8 -*-__author__ = 'EchoRep'li = [11,22,33,44]#定義一個主函數def main(): print filter(lambda a:a>33,li) pass#限制程序入口if __name__ == "__main__": main()
用于累計操作,如累加,累乘等
#!/usr/bin/env python# -*- coding: utf-8 -*-__author__ = 'EchoRep'li = [1,2,3,4,5,6,7,8,9]#定義一個主函數def main(): print reduce(lambda arg1,arg2:arg1*arg2,li)#限制程序入口if __name__ == "__main__": main()
暫時跳出了函數,但是下次可以繼續使用,和continue要區分開
#!/usr/bin/env python# -*- coding: utf-8 -*-__author__ = 'EchoRep'def func1(): yield 1 yield 2 yield 3def mrange(arg): seed = 0 while True: seed = seed + 1 if seed > arg: return else: yield seed#定義一個主函數def main(): for i in mrange(10): print i#限制程序入口if __name__ == "__main__": main()
新聞熱點
疑難解答