一,内键方法
1、fromkeys(...)
语法:dict1.fromkeys(s[,v]):s指的是字典的键值,[,v]指的是可选项(值),[,v]不填写的话默认为none
#不填写第二个值,默认为none
>>> dict1 = {}
>>> dict1.fromkeys((1,2,3))
{1: None, 2: None, 3: None}
#第二个值是多个字符串时,会作为一个整体赋值给键值
>>> dict1.fromkeys((1,2,3),('one','two','three'))
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
#采用fromkeys后,不能对value进行修改,只会重新创建一个字典
例如:尝试对1,3的key进行修改为four,five
>>> dict1.fromkeys((1,3),('four','five'))
{1: ('four', 'five'), 3: ('four', 'five')}
>>>
---由代码可以得知:只会重新创建一个字典,不能对其进行修改
2、keys():返回字典的所有键,
3、value():返回字典的所有值
4、items():返回字典的整个项
---
>>> dict1 = {}
>>> dict1 = dict1.fromkeys((range(5)),('赞'))
>>> dict1
{0: '赞', 1: '赞', 2: '赞', 3: '赞', 4: '赞'}
>>> for eachKey in dict1.keys():
print(eachKey)
0
1
2
3
4
>>> for eachValue in dict1.values():
print(eachValue)
赞
赞
赞
赞
赞
>>> for eachItems in dict1.items():
print(eachItems)
(0, '赞')
(1, '赞')
(2, '赞')
(3, '赞')
(4, '赞')
>>>
5、检查字典内的键是否存在,并且不需要报错,采用get(键)并且也可以进行赋值get(键,value)
#检查dict1字典内的第五个键是否存在
>>> dict1.get(5)
>>> print(dict1.get(5))
None
--返回的是none
#对dict1字典内的第五个键进行赋值
>>> dict1.get(5,'木有!')
'木有!
6、成员资格操作符:in not in
>>> 5 in dict1
False
>>> 4 in dict1
True
7、clear():清除字典
8、copy():拷贝字典
>>> a = {1:'one',2:'two',3:'three'}
>>> b = a.copy()
>>> c = a
>>> b
{1: 'one', 2: 'two', 3: 'three'}
>>> c
{1: 'one', 2: 'two', 3: 'three'}
9、pop():截取字典的片段value
>>> a = {1:'one',2:'two',3:'three'}
>>> a.pop(2) #2指的是键值
'two'
10、popitem():随机截取字典内的数据
>>> a.popitem()
(3, 'three')
>>> a.popitem()
(1, 'one')
11、setdefault():随机增加字典内的数据
>>> a = {1:'one',2:'two',3:'three'}
>>> a.setdefault(5,'five')
'five'
>>> a
{1: 'one', 2: 'two', 3: 'three', 5: 'five'}
>>>
----
>>> a = {1:'one',2:'two',3:'three'}
>>> a.setdefault('小白')
>>> a
{1: 'one', 2: 'two', 3: 'three', '小白': None}
12、update():更新字典
>>> a = {1: 'one', 2: 'two', 3: 'three', 5: 'five'}
>> b = {'小白':'狗'}
>>> a.update(b)
>>> a
{1: 'one', 2: 'two', 3: 'three', 5: 'five', '小白': '狗'}
二、练习题
1、现在要实现一个需求,弹出一个界面,可以让用户选择是新建用户还是登录已有账户,或者直接退出,在登录账户的时候,密码输入错误不能超过三次,超过三次要回到主界面,用户名输入‘q’的时候也可以退出并回到主界面
--- 小甲鱼的代码:
---思路:
a、注册新用户,通过用户名判断是否被使用,被使用重新输入,没有被使用继续输入密码,注册成功
b、登录,通过用户判断是否存在,不存在重新输入,存在输入密码(判断密码是不是一致,一致登录成功,不一致提示输入密码错误)
c、通过不同的指令来确定进行注册,还是登陆,还是退出
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 pwd = user_data.get(name) Ntimes = 3 while Ntimes !=0: passwd = input('请输入密码:') if passwd != pwd: print ('密码输入错误,还有%d 次机会'% Ntimes) Ntimes -=1 if Ntimes ==0: print('机会已经用完!') break elif passwd == pwd: print('欢迎进入XX00系统,请点击右上角的结束程序') else: print('密码输入错误') def showsume(): 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 == 'N' or choice == 'n': new_user() if choice == 'E' or choice == 'e': old_user() if choice == 'Q' or choice == 'q': break showsume()