python新手之字典增删改查

一、字典的定义

city_list = { 'beijin':"北京",'shanghai':"上海" }

print(city_list)

二、字典添加一个元素
city_list['wuhan'] = "武汉"

三、字典修改一个元素
city_list['beijin'] = "武汉"

四、字典删除
1、删除某个键值
city_list.pop("beijin")
print(city_list)
2、随机删除某个一个元素
city_list.popitem()

3、删除所有的元素
city_list.clear()
4、删除一个值
del city_list['beijin']
五、字典的查询
print(city_list['shanghai'])
print(city_list.get('shanghai'))



六、字典的循环遍历
1、方式一:
for key  in city_list :
print(key,city_list[key])

2、方式二:
for key in city_list.keys() :
print(key, city_list[key])

3、方式三、值遍历
for value in city_list.values() :
print(value)

4、方式四 字典遍历(数量较大时候转化需要时间,建议使用1、2、3)
for it in city_list.items() :
print(it)

for key,value in city_list.items() :
print(key,value)

七、字典的其他的函数的使用:
1、fromkeys 初始化一个字典
test = dict.fromkeys('1,2,3',["aa","bb","cc"])
print (test)

2、update 函数合并两个字典,有重复部分更新字典二的值
test =  city_list.update(city_list2)
print(city_list)

3、setdefault 有值时候不改变,无值时候添加一个值
city_list2.setdefault('wuhan','武汉')
print(city_list2)

八、字典和列表的区别:
1、字典是无序的,数组是有序的。
2、字典是通过key进行查找,列表是通过键值查找
3、查询算法的复杂度不一样,list 算法复杂度为O(n),字典dict算法复杂度为O(1)
4、占用内存不一样,dict 占用内存是list1.5


九、多级字典使用
# Author : xiajinqi
city_list = {
'北京' : {
'昌平' : ["金山","百度"],
'朝阳' : ["oldboy"]
},
'深圳' : {
'南山区' : ['友金所','金蝶','腾讯','中兴'],
'罗湖区' : ['用友','海颐'],
'宝安区' : ['中国证券','天下第一']
},
'广州' : {
'天河区' : ['天河公园'],
'越秀区': ['越秀公园']
}
}
#多级字典的遍历
for i in city_list :
for j in city_list['北京'] :
print(j)


作业题目一:
手机通讯录实现增加联系人、删除联系人、查询联系人。
脚本:
# Author : xiajinqi

#需求定义一个通讯录实现增加删除查询联系人

phone_address_book = {'xiajinqi':'18125012733','wuwang':'18125012788'}

while 1 :
print(
'''
----欢迎登录手机通讯录首页-----
1、查询联系人手机号码
2、增加一个联系人或者修改一个联系人
3、删除一个联系人
4、显示当前所有联系人
'''
)

user_choose = input("choose :")
if user_choose.isdigit() :
user_choose = int(user_choose)
else :
print("输入的不是一个数字,请重新选择 usage;1")
continue

if user_choose > 4 or user_choose < 1 :
print("输入的值必须要在1和3之间")
continue

if user_choose == 1 :
name = input("请输入要查找的名字: ")
# 通过键值查找联系人手机号码
if name in phone_address_book :
phone = phone_address_book.get(name)
print("名字:%s,手机号码:%s" %(name,phone))
else :
print("你输入的名字在通讯录不存在")
continue
elif user_choose == 2 :
name = input("请输入要修改或者添加人员的名字: ")
phone = input("请输入手机号码:")
phone_address_book [name] = phone
elif user_choose == 3 :
name = input("请输入要删除人员的名字: ")
phone_address_book.pop(name)
elif user_choose == 4 :
for key in phone_address_book.keys() :
print("名字:%s,手机号码:%s" %(key,phone_address_book[key]))
print("当前联系人总数 %s" % (len(phone_address_book)))
else :
print("输入错误,重新选择输入")



作业题目二:
 1 #!/usr/bin/python
 2 
 3 #三级菜单,实现
 4 ################################################
 5  # Task Name: 三级菜单                           #
 6 # Description:打印省、市、县三级菜单             #
 7 # 可返回上一级                      #
 8 # 可随时退出程序                   #
 9 #  ----------------------------------------------#
10 # Author:xiajinqi                          #
11 
12 city_list = {
13     'guangzhou':{
14         'guangzhou':['越秀','天河'],
15         'shenzhen':['南山区','保安区','罗湖区']
16     },
17     'hubei' :{
18         'wuhan':['武昌区','洪山区','江岸区'],
19         'huanggang':['麻城','罗田']
20     }
21 }
22 
23 
24 province = list(city_list.keys())
25 
26 #
27 while 1 :
28     print("".center(50,'*'))
29     for pro  in   province :
30         print(province.index(pro)+1,pro)
31     user_choose = input("一级菜单,请选择>>>")
32     if user_choose.isdigit() :
33         user_choose = int(user_choose)
34         if user_choose > 0 and user_choose <= len(province)   :
35             pro_name = province[user_choose-1]
36             city = list(city_list[pro_name].keys())
37             while 1 :
38                 print("".center(50, '*'))
39                 for ct in  city :
40                     print(city.index(ct)+1,ct)
41                 city_choose = input("二级菜单,请选择>>>")
42                 if city_choose.isdigit():
43                     city_choose = int(city_choose)
44                     if city_choose > 0 and  city_choose <= len(city)  :
45                         city_name = city[city_choose -1]
46                         town= city_list[pro_name][city_name]
47                         while 1 :
48                             print("".center(50, '*'))
49                             for tn  in  town:
50                                 print(town.index(tn) +1,tn)
51                             town_choose=input("三级菜单,请选择>>>")
52                             if town_choose == 'b' :
53                                 break
54                             elif town_choose =='QUIT' :
55                                 exit()
56                             else :
57                                 print("已经是最底层菜单")
58                     else :
59                         print("选择错误,请重新输入")
60                         continue
61 
62                 elif city_choose == 'b':
63                     print("回退")
64                     break
65                 elif city_choose =='QUIT' :
66                     exit()
67                 else :
68                     print("输入错误,请重新输入")
69         else :
70             print("输入错误,请重新输入")
71     elif user_choose == 'QUIT' :
72         exit()
73     else :
74         print("输入不合法")
75         continue

 












 









 



posted @ 2018-03-28 23:23  马里亚纳仰望星空  Views(353)  Comments(0Edit  收藏  举报