字典的基本方法
什么是字典:
字典是一种 key - value的数据类型,听alex说就像我们上学用的字典,通过笔划,字母来查找对饮页面的详细内容。
语法:
id_dict = { 'stu1101': "TengLan Wu", 'stu1102': "LongZe Luola", 'stu1103': "XiaoZe Maliya", }
字典的特性:
dict是无序的
key必须是唯一的,value可以重复, key=键,value=值
增加:
id_dict["stu1104"] = "smelond" print(id_dict) {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1104': 'smelond'}
修改:
id_dict["stu1101"] = "amanda" print(id_dict) {'stu1101': 'amanda', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
删除里面的某一项:
print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
id_dict.pop("stu1101")#标准删除
print(id_dict)
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
del删除:
print(id_dict) {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} del id_dict["stu1101"]#del删除 print(id_dict) {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
随机删除:
print(id_dict) {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} id_dict.popitem()#随机删除 print(id_dict) {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola'}
直接删字典:
print(id_dict) id_dict.clear() #删除字典里面的所有内容 print(id_dict) {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} {}
查找:
print("stu1101" in id_dict)#in查看有没有这个对象 True #如果有返回真,没有则fFalse
获取:
print(id_dict.get("stu1101"))#用get获取如果存在返回key值,不存在则返回None, TengLan Wu
print(id_dict["stu1101"])#这个方法不会像上面那样智能,key不存在就直接报错 TengLan Wu print(id_dict["stu11231"]) print(id_dict["stu11231"]) KeyError: 'stu11231'
多字典嵌套:
#!/usr/bin/env python # _*_ coding:utf-8 _*_ # File_type:多字典嵌套 # Filename:dict_nest.py # Author:smelond id_dict = { "ChengDu": { "acreage": 14312, "population": "1591.8w", "postalcode": 610000}, "ShenZhen": { "acreage": 1196, "population": "1190.08w", "postalcode": 518000}, "BeiJing": { "acreage": "1.641w", "population": "2172.9w", "postalcode": 100000} } print(id_dict["ChengDu"])#打印输出所有的值
print(id_dict["ChengDu"]["acreage"]) #打印输出成都的面积
id_dict["ChengDu"]["acreage"] = "面 积:14312平方千米" #给成都的面积重新修改为了"面 积:14312平方千米"
print(id_dict["ChengDu"]["acreage"])#打印
{'acreage': 14312, 'population': '1591.8w', 'postalcode': 610000}
14312
面 积:14312平方千米
将字典转换为元组:
print(id_dict.items()) dict_items([('stu1101', 'TengLan Wu'), ('stu1102', 'LongZe Luola'), ('stu1103', 'XiaoZe Maliya')])
将字典转换为列表:
list_test = list(id_dict) print(list_test) ['stu1101', 'stu1102', 'stu1103']
字典的循环:
for key in id_dict: print(key, id_dict[key])#由于加入了key,所以他把stu也循环出来了 输出: stu1101 TengLan Wu stu1102 LongZe Luola stu1103 XiaoZe Maliya for key in id_dict: print(id_dict[key]) 输出: TengLan Wu LongZe Luola XiaoZe Maliya
多字典嵌套的循环:
#!/usr/bin/env python # _*_ coding:utf-8 _*_ # File_type:多字典嵌套 # Filename:dict_nest.py # Author:smelond id_dict = { "ChengDu": { "acreage": 14312, "population": "1591.8w", "postalcode": 610000}, "ShenZhen": { "acreage": 1196, "population": "1190.08w", "postalcode": 518000}, "BeiJing": { "acreage": "1.641w", "population": "2172.9w", "postalcode": 100000} } for key in id_dict:#还是照常循环 test = id_dict[key]#将我们每次的key赋给test for key in test:#再来循环test # print(key, ":", test[key], "\n", end="") print("%s : %s" % (key, test[key]))#将第二级的key打印,并且打印出value acreage : 14312 population : 1591.8w postalcode : 610000 acreage : 1196 population : 1190.08w postalcode : 518000 acreage : 1.641w population : 2172.9w postalcode : 100000