Python3 的字典
程序员文章站
2022-04-16 09:29:40
1、dict() 字典 字典是python里唯一的映射类型 2、字典由key和value组成的项组成 如何创建一个字典: 3、字典的内置函数 keys values items copy clear get fromkeys update pop popitems setdefault 4、设计一个 ......
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()
上一篇: 日常刷题
下一篇: 删除排序数组中的重复项