Python3 的字典,Python3字典
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) onetwothree
values
>>> b = {'one': 1, 'two': 2, 'three': 3}>>> for i in b.values(): print(i) 123
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=0flag1=0flag2=0while 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()