小甲鱼python的课后题好难_小甲鱼《零基础学习Python》课后笔记(二十六):字典——当索引不好用时2...
測(cè)試題
0.Python的字典是否支持一鍵(Key)多值(Value)?
不支持。對(duì)相同的鍵賦值會(huì)覆蓋原來(lái)的值。>>> dict2 = {1:'one',1:'two',3:'three'}
>>> dict2
{1: 'two', 3: 'three'}
1.在字典中,如果試圖為一個(gè)不存在的鍵(Key)賦值會(huì)怎樣?
會(huì)創(chuàng)建一個(gè)新的鍵值對(duì)。>>> 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)可以檢測(cè)一個(gè)元素是否在序列中,當(dāng)然也可以用來(lái)檢查一個(gè)鍵(Key)是否在字典中。那么請(qǐng)問(wèn)哪種的檢查效率更高些?為什么?
字典的效率要更高一些。因?yàn)樽值涞脑硎鞘褂霉K惴ù鎯?chǔ),不需要使用查找算法進(jìn)行匹配,時(shí)間復(fù)雜度是O(1)。
3.Python對(duì)鍵(Key)和值(Value)有沒(méi)有類型限制?
Python對(duì)鍵有要求,要求是可哈希(Hash)的對(duì)象,不能是可變類型(包括變量,列表,字典本身等)
對(duì)于值就沒(méi)有任何限制,可以是Python里的任何類型。
4.請(qǐng)目測(cè)下邊代碼執(zhí)行后,字典dict1的內(nèi)容是什么?dict1.fromkeys((1,2,3),('one', 'two', 'three'))
dict1.fromkeys((1,3), '數(shù)字')
dict1的內(nèi)容不變,保持原來(lái)的內(nèi)容。>>> 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), '數(shù)字')
{1: '數(shù)字', 3: '數(shù)字'}
>>> dict1
{}
要注意fromkeys()方法是返回一個(gè)新創(chuàng)建的字典。>>> 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), '數(shù)字')
>>> dict2
{1: '數(shù)字', 3: '數(shù)字'}
>>> dict1
{}
5.如果你需要將字典dict1 = {1: ‘one’,2: ‘two’,3: ‘three’}拷貝到dict2,你應(yīng)該怎么做? 使用copy()方法。不要使用賦值等號(hào)。>>> 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'}
賦值等號(hào)是把一個(gè)字典指向內(nèi)存地址而已,所以改變dict3也就是操作和dict1一樣的內(nèi)存。
動(dòng)動(dòng)手
0.嘗試編寫(xiě)一個(gè)用戶登錄程序(這次嘗試將功能封裝成函數(shù)),程序?qū)崿F(xiàn)如圖:def Log_On():
'用戶登錄程序'
dict1 = {}
while True:
print('|---新建用戶:N/n---|')
print('|---登錄賬號(hào):E/e---|')
print('|---推出程序:Q/q---|')
number =input('|---請(qǐng)輸入指令代碼:')
if (number == 'N') or (number == 'n'):
key = input('請(qǐng)輸入用戶名:')
for x in range(4):
if key in dict1:
key = input('此用戶名已被使用,請(qǐng)重新輸入:')
value = input('請(qǐng)輸入密碼:')
dict1[key] = value
print('注冊(cè)成功,趕緊試試登錄吧^_^')
break
else:
value = input('請(qǐng)輸入密碼:', )
dict1[key] = value
print('注冊(cè)成功,趕緊試試登錄吧^_^')
break
continue
elif (number == 'E') or (number == 'e'):
key = input('請(qǐng)輸入用戶名:')
if key in dict1:
for x in range(3):
value = input('請(qǐng)輸入密碼:')
if dict1[key] == value:
print('歡迎進(jìn)入WOLF系統(tǒng),點(diǎn)擊右上角的X結(jié)束程序!')
break
else:
if x < 2:
print('出錯(cuò)提示:密碼錯(cuò)誤,請(qǐng)您重新登錄!')
continue
else:
break
continue
else:
for x in range(3):
key = input('你輸入的用戶名不存在,請(qǐng)重新輸入:')
if key in dict1:
value = input('請(qǐng)輸入密碼:')
print('歡迎進(jìn)入WOLF系統(tǒng),點(diǎn)擊右上角的X結(jié)束程序!')
break
else:
if x < 2:
print('不存在此用戶名,請(qǐng)檢查書(shū)寫(xiě),重新輸入:')
continue
continue
elif (number == 'Q') or (number == 'q'):
break
else:
print('輸入錯(cuò)誤!')
continue
Log_On()
總結(jié)
以上是生活随笔為你收集整理的小甲鱼python的课后题好难_小甲鱼《零基础学习Python》课后笔记(二十六):字典——当索引不好用时2...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python爬取学校题库_pyhton
- 下一篇: 为什么攒不下钱了 这三个阶段的人最烧钱