1 # 1.用你能想到的最少的代码生成一个包含100个key的字典,每个value的值不能一样
 2 '''
 3 # import random
 4 key = {}
 5 for i in range(100):
 6     key.setdefault(i,i)
 7     # key.setdefault(random.randrange(101),random.randrange(101))
 8 # set key[range(100)= range(100) if range(100) not in key]
 9 print(key)
10 # set D[k]=d if k not in D
11 '''
12 
13 # 2.{'k0': 0, 'k1': 1, 'k2': 2, 'k3': 3, 'k4': 4, 'k5': 5, 'k6': 6, 'k7': 7, 'k8': 8, 'k9': 9}
14 # 请把这个dict中key大于5的值value打印出来。
15 '''
16 1.
17 dic = {'k0': 0, 'k1': 1, 'k2': 2, 'k3': 3, 'k4': 4, 'k5': 5, 'k6': 6, 'k7': 7, 'k8': 8, 'k9': 9}
18 for k in dic:
19     if dic[k] > 5:
20         print(dic[k])
21     # print(lambda dic[k]:dic[k] > 5)
22 2.
23 a = [dic[k] for k in dic if dic[k] >5]
24 print(a)
25 '''
26 # 3.把题2中value是偶数的统一改成-1
27 '''
28 dic = {'k0': 0, 'k1': 1, 'k2': 2, 'k3': 3, 'k4': 4, 'k5': 5, 'k6': 6, 'k7': 7, 'k8': 8, 'k9': 9}
29 for k in dic:
30     if dic[k] %2 ==0:
31         dic[k] = -1
32 print(dic)
33 '''
34 # 4.请设计一个dict, 存储你们公司每个人的信息,信息包含至少姓名、年龄、电话、职位、工资,并提供一个简单的查找接口,
35 #     用户按你的要求输入要查找的人,你的程序把查到的信息打印出来
36 # info={
37 #     {'name':'张三0','age':20,'电话':5879960,'职位':'员工0','工资':'5000'},
38 #     {'name':'张三1','age':21,'电话':5879961,'职位':'员工1','工资':'5001'},
39 #     {'name':'张三2','age':22,'电话':5879962,'职位':'员工2','工资':'5002'},
40 #     {'name':'张三3','age':23,'电话':5879963,'职位':'员工3','工资':'5003'},
41 #     {'name':'张三4','age':24,'电话':5879964,'职位':'员工4','工资':'5004'},
42 #     {'name':'张三5','age':25,'电话':5879965,'职位':'员工5','工资':'5005'},
43 #     {'name':'张三6','age':26,'电话':5879966,'职位':'员工6','工资':'5006'},
44 #     {'name':'张三7','age':27,'电话':5879967,'职位':'员工7','工资':'5007'},
45 #     {'name':'张三8','age':28,'电话':5879968,'职位':'员工8','工资':'5008'},
46 #     {'name':'张三9','age':29,'电话':5879969,'职位':'员工9','工资':'5009'},
47 # }
48 
49 info = {
50     '张三0': {'age': 20, '电话': 5879960, '职位': '员工0', '工资': '5000'},
51     '张三1': {'age': 21, '电话': 5879961, '职位': '员工1', '工资': '5001'},
52     '张三2': {'age': 22, '电话': 5879962, '职位': '员工2', '工资': '5002'},
53     '张三3': {'age': 23, '电话': 5879963, '职位': '员工3', '工资': '5003'},
54     '张三4': {'age': 24, '电话': 5879964, '职位': '员工4', '工资': '5004'},
55     '张三5': {'age': 25, '电话': 5879965, '职位': '员工5', '工资': '5005'},
56     '张三6': {'age': 26, '电话': 5879966, '职位': '员工6', '工资': '5006'},
57     '张三7': {'age': 27, '电话': 5879967, '职位': '员工7', '工资': '5007'},
58     '张三8': {'age': 28, '电话': 5879968, '职位': '员工8', '工资': '5008'},
59     '张三9': {'age': 29, '电话': 5879969, '职位': '员工9', '工资': '5009'},
60 }
61 print("输入查询方式,name or tel?")
62 search_way = input(">>> ")
63 if search_way == 'name':
64     name = input("input name :")
65     print(info.get(name, "输出错误,没找到这个人"))#没有这个key,就返回默认值
66 elif search_way == 'tel':
67     tel = int(input("input tel: "))
68     for i in info:
69         # break
70         for v in info[i].items():
71             # print(type(v))
72             if tel in v:
73                 print("name: ", i, info[i])
74             else:
75                 print("Wrong telphone number.")
76 else:
77     print("inputing is wrong...")

 

posted on 2019-07-05 17:49  嗯哈哈  阅读(745)  评论(0编辑  收藏  举报