python 小甲鱼 好不好_[Python]小甲鱼Python视频第025课(字典:当索引不好用时)课后题及参考解答...
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 8 10:04:08 2019
@author: Administrator
"""
"""
測試題:
0. 當你聽到小伙伴們在談論“映射”、“哈希”、“散列”或者“關系數組”的時候,事實上他們就是在討論什么呢?
和字典的特性都有關。。。
1. 嘗試一下將數據('F': 70, 'C': 67, 'h': 104, 'i': 105, 's': 115)創建為一個字典并訪問鍵 'C' 對應的值?
2. 用方括號(“[]”)括起來的數據我們叫列表,那么使用大括號(“{}”)括起來的數據我們就叫字典,對嗎?
不對,{}括起來也可能是集合,{}括起來的鍵值對才是字典
3. 你如何理解有些東西字典做得到,但“萬能的”列表卻難以實現(臣妾做不到T_T)?
列表的元素索引是固定的數字序列, 字典的元素索引就是keys,元素類型較為靈活,但相應的效率會降低
4. 下邊這些代碼,他們都在執行一樣的操作嗎?你看得出差別嗎?
>>> 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})
a. 使用關鍵字參數創建字典
b. 用{}包裹鍵值對創建字典
c. 利用zip縫合成zip對象傳遞給工廠函數dict創建字典
d. 利用二元元組對列表傳遞給工廠函數創建字典
e. 用字典初始化字典
5. 如圖,你可以推測出打了馬賽克部分的代碼嗎?
動動手:
0. 嘗試利用字典的特性編寫一個通訊錄程序吧,功能如圖:
"""
#測試題1.
dict1 = dict({'F': 70, 'C': 67, 'h': 104, 'i': 105, 's': 115});
print(dict1['C']);
#測試題5
data = "1000,小甲魚,男";
MyDict = {};
(MyDict['id'],MyDict['name'],MyDict['sex']) = data.split(sep=',');
print("ID: " + MyDict['id']);
print("Name: " + MyDict['name']);
print("Sex: " + MyDict['sex']);
#動動手0
string1 = """
|--- 歡迎進入通訊錄程序 ---|
|--- 1:查詢聯系人資料 ---|
|--- 2:插入新的聯系人 ---|
|--- 3:刪除已有聯系人 ---|
|--- 4:退出通訊錄程序 ---|
"""
print(string1);
txl = dict();
while True:
int_input = int(input('\n請輸入相關的指令代碼:'));
if int_input == 1:
name = input("請輸入聯系人姓名:");
if name in txl.keys():
print("%s:%s" %(name,txl[name]));
else:
print("通訊錄沒有 %s 的通訊信息" % name );
elif int_input == 2:
name = input("請輸入聯系人姓名:");
if name in txl.keys():
print('您輸入的姓在通訊錄中已經存在 -->>',end='');
print(name + ":" + txl[name]);
if input('是否修改現有用戶資料(Y/N)' == 'Y'):
txl[name] = input('請輸入更新后的聯系電話:');
else:
txl[name] = input('請輸入用戶的聯系電話:')
elif int_input == 3:
name = input("請輸入聯系人姓名:");
if name in txl.keys():
del(txl[name]);
else:
print('輸入的聯系人不存在.');
elif int_input == 4:
break;
else:
print('輸入指令代碼有誤');
總結
以上是生活随笔為你收集整理的python 小甲鱼 好不好_[Python]小甲鱼Python视频第025课(字典:当索引不好用时)课后题及参考解答...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做试管二代直接移植刚复苏的冻胚简单生化妊
- 下一篇: 腾讯视频如何给梦华录评分