Chapter7.映像和集合類型python
最近臨到期末,真的被各類複習,各類大做業纏住,想一想已經荒廢了python的學習1個月了。如今失去了昔日對python的觸覺和要寫簡潔優雅代碼的感受,因此臨到期末毅然繼續python的學習,還特意花了一個小時把以前寫的代碼和筆記再看了一遍,我想若是再晚一點的話連python是何物都恐怕不知道了!算法
這一章的習題不知道咋樣?可是無論了, let's go !安全
7.1哪一個字典方法能夠用來把兩個字典合併在一塊兒?ide
在命令行下輸了help({}),看了下dist的內建方法。發現只有update方法比較貼近,看一下update方法:函數
update(...) | D.update([E, ]**F) -> None. Update D from dict/iterable E and F. | If E present and has a .keys() method, does: for k in E: D[k] = E[k] | If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v | In either case, this is followed by: for k in F: D[k] = F[k]
可是對於兩個鍵徹底不一樣的字典,update方法會將它們合併,可是對於存在部分鍵相同的字典,update只會起到一個更新做用;學習
1 >>> dist1={'a':1,'b':2} 2 >>> dist2={'c':3,'d':4} 3 >>> dist3={'a':5,'e':6} 4 >>> dist2.update(dist1) 5 >>> dist2 6 {'a': 1, 'c': 3, 'b': 2, 'd': 4} 7 >>> dist3.update(dist1) 8 >>> dist3 9 {'a': 1, 'b': 2, 'e': 6} 10 >>> dist1.update(dist3) 11 >>> dist1 12 {'a': 1, 'b': 2, 'e': 6}
可是順序不一樣,更新也不一樣,ui
1 >>> dist1={'a':1,'b':2} 2 >>> dist2={'c':3,'d':4} 3 >>> dist3={'a':5,'e':6} 4 >>> dist1.update(dist3) 5 >>> dist1 6 {'a': 5, 'b': 2, 'e': 6}
基本上內建的字典方法就只有這個了吧,可能要功能好一點的話估計得本身寫了。this
7.3.字典和列表的方法:加密
(a).建立一個字典,並把字典中的鍵按照字母順序顯示出來。spa
(b).按照字母順序排序好的鍵,顯示出這個字典中的鍵和值。
(c).按照字母順序排序好的值,顯示出這個字典中的鍵和值。
應該不難吧。。。。。難就難在經過字典的值來找鍵沒有現成的函數吧。
1 #!usr/bin/env python 2 #-*-coding=utf-8-*- 3 4 import string 5 6 ss = string.lowercase 7 dict1 = dict([(ss[i-1],26+1-i) for i in range(1,27)]) 8 print 'dictionary is :' 9 print dict1 10 dict2 = sorted(dict1.keys()) 11 print '(a) :',dict2 12 print '(b) :' 13 for key in dict2: 14 print 'key = %s , value = %d ' % (key,dict1[key]) 15 16 dict3 = sorted(dict1.values()) 17 print '(c) :' 18 for value in dict3: 19 for (u,v) in dict1.items(): 20 if value == v: 21 print 'key = %s , value = %d' % (u,v)
7.4.創建字典。用兩個相同長度的列表組建一個字典出來。幾行能寫出來。
1 #!usr/bin/env python 2 #-*-coding=utf-8-*- 3 4 def merge(list1,list2): 5 return dict(zip(list1,list2)) 6 7 if __name__ == '__main__': 8 list1 = [1,2,3] 9 list2 = ['abc','def','ghi'] 10 print merge(list1,list2)
7.5.按照題目來修改userpw.py,要用到一些外部的模塊
(a).用到time模塊的函數,顯示登陸時間。還要要求相差不超太小時,要顯示出來。主要仍是用到localtime()
(b).添加一個管理菜單,實現如下兩種功能:(1).刪除一個用戶;(2).顯示系統中全部用戶的名字和他們密碼的清單。
應該感受不難,主要對字典進行操做便可。
(c).對口令進行加密操做,不難,只須要對用戶的密碼統一加密成sha1(安全哈希算法)碼便可。(使用sha模塊)
(d).添加圖形界面(先挖坑,之後再補)
(e).要求用戶名不區分大小寫。能夠選擇先把用戶名統一保存爲小寫形式(或大寫形式),驗證時也將用戶輸入的同一轉化爲小寫(或大寫)來進行覈對。
(f).增強對用戶名的限制,不容許符合和空白符。對於字符先後的空白符能夠經過使用strip()來消除。其餘估計要用到正則來解決。
(g).合併新用戶和老用戶功能。不難。
1 #!/usr/bin/env python 2 #-*-coding=utf-8-*- 3 4 import time #導入time模塊 5 import sha #導入sha模塊 6 import string #導入string模塊 7 import re #導入re模塊(正則匹配) 8 9 db = {} #創建字典 10 11 def newuser(): 12 prompt = 'login desired : ' 13 while True: 14 name = raw_input(prompt) 15 k = re.search(r'\W+',name) 16 if k != None: 17 prompt = 'Your name contains illegal characters,try another: ' 18 continue 19 if db.has_key(name): 20 prompt = 'name taken,try another: ' 21 continue 22 else: 23 break 24 pwd = raw_input('password: ') 25 hash = sha.new() #新建哈希表 26 hash.update(pwd) 27 psd = hash.hexdigest() #Sha-1碼 28 lt = list(time.localtime()[:-3]) #定義如今的時間戳 29 info = [psd,lt] #創建用戶的我的信息(沒有結構體真蛋疼) 30 db[string.lower(name)] = info 31 print "Logining in %d/%d/%d -- %d:%d:%d" % (info[1][0],info[1][1],info[1][2],info[1][3],info[1][4],info[1][5]) 32 33 def olduser(): 34 while True: 35 name = raw_input('login: ') 36 k = re.search(r'\W+',name) 37 if k != None: 38 prompt = 'Your name contains illegal characters,try another: ' 39 continue 40 else: 41 break 42 if db.has_key(string.lower(name)) == False: 43 c = raw_input('This username does not exist.Do you want to create a user ?') 44 if string.lower(c) == 'y': 45 pwd = raw_input('password: ') 46 hash = sha.new() #新建哈希表 47 hash.update(pwd) 48 psd = hash.hexdigest() #Sha-1碼 49 lt = list(time.localtime()[:-3]) #定義如今的時間戳 50 info = [psd,lt] #創建用戶的我的信息(沒有結構體真蛋疼) 51 db[string.lower(name)] = info 52 print "Logining in %d/%d/%d -- %d:%d:%d" % (info[1][0],info[1][1],info[1][2],info[1][3],info[1][4],info[1][5]) 53 else: 54 pwd = raw_input('password: ') 55 hash = sha.new() 56 hash.update(pwd) 57 psd = hash.hexdigest() 58 info = db.get(string.lower(name)) 59 temp = list(time.localtime()[:-3]) #更新時間 60 if info[0] == psd: 61 print 'welcome back',string.lower(name) 62 print "Logining in %d/%d/%d -- %d:%d:%d" % (temp[0],temp[1],temp[2],temp[3],temp[4],temp[5]) 63 if(abs(info[1][3] - temp[3]) < 4): 64 print "You already logged in at:%d/%d/%d -- %d:%d:%d" % (info[1][0],info[1][1],info[1][2],info[1][3],info[1][4],info[1][5]) 65 info[1] = temp 66 else: 67 print 'login incorrect' 68 69 def manage(): 70 #設定用戶權限 71 while True: 72 name = raw_input('login(root): ') 73 k = re.search(r'\W+',name) 74 if k != None: 75 prompt = 'Your name contains illegal characters,try another: ' 76 continue 77 else: 78 break 79 pwd = raw_input('password: ') 80 if pwd != 'root': 81 print 'login(root) incorrect!' 82 else: 83 prompt = """ 84 (D)elete A User 85 (S)how All Users 86 87 Enter choice:""" 88 choice = raw_input(prompt).strip()[0].lower() 89 if choice == 'd': 90 user = raw_input('Please input user:') 91 if db.has_key(user): 92 db.pop(user) 93 print 'Finish!' 94 else: 95 print 'No such user.' 96 if choice == 's': 97 print 'All users are as follows:' 98 for key in db.keys(): 99 print key 100 101 def showmenu(): 102 prompt = """ 103 (N)ew User Login 104 (E)xisting User Login 105 (Q)uit 106 (M)anage 107 108 Enter choice:""" 109 110 done = False 111 while not done: 112 chosen = False 113 while not chosen: 114 try: 115 choice = raw_input(prompt).strip()[0].lower() 116 except (EOFError,KeyboardInterrupt): 117 choice = 'q' 118 print '\nYou picked:[%s]' % choice 119 if choice not in 'neqm': 120 print 'invalid option,try again' 121 else: 122 chosen = True 123 if choice == 'q':done = True 124 if choice == 'n':newuser() 125 if choice == 'e':olduser() 126 if choice == 'm':manage() 127 128 if __name__ == '__main__': 129 showmenu()
7-7.顛倒字典中的鍵和值,用一個字典作輸入,輸出另外一個字典,用前者的鍵作值,用前者的值作鍵。
將前者字典的鍵遍歷一遍便可。
1 #!usr/bin/env python 2 #-*-coding=utf-8-*- 3 4 def change(before): 5 after = {} 6 for key in before.keys(): 7 after[before[key]] = key 8 return after 9 10 if __name__ == '__main__': 11 dic = {'x':1,'y':2} 12 print change(dic)
7-9.能夠用re模塊的sub函數輕鬆解決,這裏就不用字典來多餘實現了。
字典這章裏面有趣題目都比較少,先湊合吧。
就這樣,請多多指教!