小甲鱼《零基础学习Python》课后笔记(二十六):字典——当索引不好用时2

测试题
0.Python的字典是否支持一键(Key)多值(Value)?
不支持。对相同的键赋值会覆盖原来的值。

>>> dict2 = {1:'one',1:'two',3:'three'}  
>>> dict2  
{1: 'two', 3: 'three'}  

1.在字典中,如果试图为一个不存在的键(Key)赋值会怎样?
会创建一个新的键值对。

>>> dict1 = {1:'one',2:'two',3:'three'}  
>>> dict1  
{1: 'one', 2: 'two', 3: 'three'}  
>>> dict1[4] = ('four')  
>>> dict1  
{1: 'one', 2: 'two', 3: 'three', 4: 'four'} 

2.成员资格符(in和not in)可以检测一个元素是否在序列中,当然也可以用来检查一个键(Key)是否在字典中。那么请问哪种的检查效率更高些?为什么?
字典的效率要更高一些。因为字典的原理是使用哈希算法存储,不需要使用查找算法进行匹配,时间复杂度是O(1)。

3.Python对键(Key)和值(Value)有没有类型限制?

Python对键有要求,要求是可哈希(Hash)的对象,不能是可变类型(包括变量,列表,字典本身等)
对于值就没有任何限制,可以是Python里的任何类型。


4.请目测下边代码执行后,字典dict1的内容是什么?

dict1.fromkeys((1,2,3),('one', 'two', 'three'))  
dict1.fromkeys((1,3), '数字')  

dict1的内容不变,保持原来的内容。

>>> dict1 = {}  
>>> dict1.fromkeys((1,2,3),('one', 'two', 'three'))  
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}  
>>> dict1.fromkeys((1,3), '数字')  
{1: '数字', 3: '数字'}  
>>> dict1  
{}  

要注意fromkeys()方法是返回一个新创建的字典。

>>> dict2 = dict1.fromkeys((1,2,3),('one', 'two', 'three'))  
>>> dict2  
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}  
>>> dict2 = dict1.fromkeys((1,3), '数字')  
>>> dict2  
{1: '数字', 3: '数字'}  
>>> dict1  
{}  
5.如果你需要将字典dict1 = {1: ‘one’,2: ‘two’,3: ‘three’}拷贝到dict2,你应该怎么做? 使用copy()方法。不要使用赋值等号。
>>> dict1 = {1:'one',2: 'two',3: 'three'}  
>>> dict2 = dict1.copy()  
>>> dict3 = dict1  
>>> dict1  
{1: 'one', 2: 'two', 3: 'three'}  
>>> dict2  
{1: 'one', 2: 'two', 3: 'three'}  
>>> dict3  
{1: 'one', 2: 'two', 3: 'three'}  
>>> dict3[4] = 'four'  
>>> dict1  
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}  
>>> dict2  
{1: 'one', 2: 'two', 3: 'three'}  
>>> dict3  
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}  
>>> dict2[4] = 'five'  
>>> dict1  
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}  
>>> dict2  
{1: 'one', 2: 'two', 3: 'three', 4: 'five'}  
>>> dict3  
{1: 'one', 2: 'two', 3: 'three', 4: 'four'} 

赋值等号是把一个字典指向内存地址而已,所以改变dict3也就是操作和dict1一样的内存。

动动手
0.尝试编写一个用户登录程序(这次尝试将功能封装成函数),程序实现如图:

def Log_On():
	'用户登录程序'
	dict1 = {}
	while True:
		print('|---新建用户:N/n---|')
		print('|---登录账号:E/e---|')
		print('|---推出程序:Q/q---|')
		number =input('|---请输入指令代码:')
		if (number == 'N') or (number == 'n'):
			key = input('请输入用户名:')
			for x in range(4):
				if key in dict1:
					key = input('此用户名已被使用,请重新输入:')
					value = input('请输入密码:')
					dict1[key] = value
					print('注册成功,赶紧试试登录吧^_^')
					break
				else:
					value = input('请输入密码:', )
					dict1[key] = value
					print('注册成功,赶紧试试登录吧^_^')
					break
			continue

		elif (number == 'E') or (number == 'e'):
			key = input('请输入用户名:')
			if key in dict1:
				for x in range(3):
					value = input('请输入密码:')
					if dict1[key] == value:
						print('欢迎进入WOLF系统,点击右上角的X结束程序!')
						break
					else:
						if x < 2:
							print('出错提示:密码错误,请您重新登录!')
							continue
						else:
							break
				continue

			else:
				for x in range(3):
					key = input('你输入的用户名不存在,请重新输入:')
					if key in dict1:
						value = input('请输入密码:')
						print('欢迎进入WOLF系统,点击右上角的X结束程序!')
						break
					else:
						if x < 2:
							print('不存在此用户名,请检查书写,重新输入:')
						continue
				continue

		elif (number == 'Q') or (number == 'q'):
			break

		else:
			print('输入错误!')
			continue
Log_On()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值