Python3 的字典
阿新 • • 發佈:2018-01-29
一個用戶 and 程序 otherwise 打印 結束程序 number dict con
1、dict() 字典
字典是python裏唯一的映射類型
2、字典由key和value組成的項組成
如何創建一個字典:
>>> a = dict(one=1, two=2, three=3) >>> b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> c = dict(zip([‘one‘, ‘two‘, ‘three‘], [1, 2, 3])) >>> d = dict([(‘two‘, 2), (‘one‘, 1), (‘three‘, 3)]) >>> e = dict({‘three‘: 3, ‘one‘: 1, ‘two‘: 2})
3、字典的內置函數
keys
>>> b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> for i in b.keys(): print(i) one two three
values
>>> b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> for i in b.values(): print(i) 1 2 3
items
>>> b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> for i in b.items() >>> for i in b.items(): print(i) (‘one‘, 1) (‘two‘, 2) (‘three‘, 3)
copy
>>> b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> c=b.copy() >>> c {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> b[‘one‘]=4 >>> b {‘one‘: 4, ‘two‘: 2, ‘three‘: 3} >>> c {‘one‘: 1, ‘two‘: 2, ‘three‘: 3}
clear
{‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> b.clear() >>> b {}
get
>>> b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> b.get(‘one‘) 1 >>> b.get(4) >>> print(b.get(4)) None
fromkeys
>>> b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> c=b.fromkeys("1",2) >>> c {‘1‘: 2}
update
>>> b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> c=b.fromkeys("1",2) >>> c {‘1‘: 2} >>> b.update(c) >>> b {‘one‘: 1, ‘two‘: 2, ‘three‘: 3, ‘1‘: 2}
pop
>>> b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> c=b.fromkeys("1",2) >>> c {‘1‘: 2} >>> b.update(c) >>> b {‘one‘: 1, ‘two‘: 2, ‘three‘: 3, ‘1‘: 2} >>> b.pop(‘1‘) 2 >>> b {‘one‘: 1, ‘two‘: 2, ‘three‘: 3}
popitems
b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> b.popitem() (‘three‘, 3)
setdefault
‘‘‘Help on built-in function setdefault: setdefault(key, default=None, /) method of builtins.dict instance Insert key with a value of default if key is not in the dictionary. Return the value for key if key is in the dictionary, else default.‘‘‘ >>> b={} >>> b.setdefault(‘1‘,2) 2 >>> b {‘1‘: 2} >>> b[‘1‘]=3 >>> b.setdefault(‘1‘,2) 3 >>> b {‘1‘: 3}
4、設計一個通訊錄程序
print("|---歡迎進入通訊錄程序---|\n|---1.查詢聯系人資料---|\n|---2.插入新的聯系人---|\n|---3.刪除已有聯系人---|\\n|---4.打印所有用戶信息---|\n|---5.退出通訊錄程序---|") mydict={} while 1: fun=input("\n請輸入相關指令代碼:") if fun==‘2‘: name=input("請輸入聯系人姓名:")#key if name in mydict: print("您輸入的用戶名已存在-->>",end=‘‘) print(name,‘:‘,mydict[name]) yn=input("是否修改用戶資料(YES/NO):") if yn == "YES": number=input("請輸入用戶電話號碼:")#value mydict[name]=number continue else: continue number=input("請輸入用戶電話號碼:")#value mydict[name]=number print(‘錄入成功!‘,name,‘:‘,mydict[name]) continue elif fun==‘1‘: name=input("請輸入聯系人姓名:")#key if name in mydict: print(name,‘:‘,mydict[name]) continue else: print("你查找的用戶不存在!") continue elif fun==‘3‘: name=input("請輸入聯系人姓名:")#key if name in mydict: print(‘用戶信息:‘,name,‘:‘,mydict[name]) if mydict.pop(name,1)!=1: print(‘刪除成功!‘) continue else: print("你刪除的用戶不存在!") continue elif fun==‘5‘: print("---感謝使用通訊錄程序---") break elif fun==‘4‘: for i in mydict: print(i,‘:‘,mydict[i],end=‘\n‘) else: print("請輸入正確指令!!!") continue ‘‘‘ Help on built-in function pop: pop(...) method of builtins.dict instance D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised ‘‘‘
5、設計一個用戶登陸程序
版本1
user={} flag=0 flag1=0 flag2=0 while 1: if flag2==1: print("歡迎進入XXOO系統,請點擊右上角的X結束程序!") while 1: flag2==0 print("\n|--- 新建用戶:N/n ---| \n|--- 登陸賬號:E/e ---| \n|--- 退出程序:Q/q ---|") fun=input("請輸入指令代碼:") while fun==‘N‘or fun==‘n‘: if flag==1: name=input("此用戶名已被使用,請重新輸入:") else: name=input("請輸入用戶名:") if name not in user: flag=0 print("用戶名可以使用!\n") pswd=input("請輸入密碼:") user[name]=pswd print("註冊成功,趕緊試試登陸吧!") break else : flag=1 continue while fun==‘E‘ or fun==‘e‘: if flag1: name=input("您輸入的用戶名不存在請重新輸入:") else: name=input("請輸入用戶名:") if name not in user: flag1=1 continue else: flag1=0 pswd=input(‘請輸入密碼:‘) if pswd==user[name]: flag2=1 break else: print("密碼錯誤") break if fun==‘Q‘ or fun==‘q‘: print("|--- 感謝使用 ---|") break
版本2
user_data = {} def new_user(): prompt = ‘請輸入用戶名:‘ while True: name = input(prompt) if name in user_data: prompt = ‘此用戶名已經被使用,請重新輸入:‘ continue else: break passwd = input(‘請輸入密碼:‘) user_data[name] = passwd print(‘註冊成功,趕緊試試登錄吧^_^‘) def old_user(): prompt = ‘請輸入用戶名:‘ while True: name = input(prompt) if name not in user_data: prompt = ‘您輸入的用戶名不存在,請重新輸入:‘ continue else: break passwd = input(‘請輸入密碼:‘) pwd = user_data.get(name) if passwd == pwd: print(‘歡迎進入XXOO系統,請點右上角的X結束程序!‘) else: print(‘密碼錯誤!‘) def showmenu(): prompt = ‘‘‘ |--- 新建用戶:N/n ---| |--- 登錄賬號:E/e ---| |--- 推出程序:Q/q ---| |--- 請輸入指令代碼:‘‘‘ while True: chosen = False while not chosen: choice = input(prompt) if choice not in ‘NnEeQq‘: print(‘您輸入的指令代碼錯誤,請重新輸入:‘) else: chosen = True if choice == ‘q‘ or choice == ‘Q‘: break if choice == ‘n‘ or choice == ‘N‘: new_user() if choice == ‘e‘ or choice == ‘E‘: old_user() showmenu()
Python3 的字典