[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('
请输入相关的指令代码:'));
    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('输入指令代码有误');

  

~不再更新,都不让我写公式,博客园太拉胯了
原文地址:https://www.cnblogs.com/alimy/p/10502951.html