python--字典

定义: 字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。

语法:value可以是任意类型。

1 info = {
2     'stu1101': "TengLan Wu",
3     'stu1102': "LongZe Luola",
4     'stu1103': "XiaoZe Maliya",
5 }

 

思考:为什么要有字典,字典的作用是什么?

因为只要记住一个key,就可以查到很多value值。比如去警察局查找某个人的时候,只要输入身份证号就可以查到姓名,家庭住址等信息。

 

特征:

  • dict是无序的
  • key必须是唯一的,so 天生去重

 

1.增加

1 >>> info["stu1104"] = "苍井空"
2 >>> info
3 {'stu1102': 'LongZe Luola', 'stu1104': '苍井空', 'stu1103': 'XiaoZe Maliya', 'stu1101': 'TengLan Wu'}

 

2.修改

1 >>> info['stu1101'] = "武藤兰"
2 >>> info
3 {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1101': '武藤兰'}

 

3.删除

 1 >>> info
 2 {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1101': '武藤兰'}
 3 >>> info.pop("stu1101") #标准删除姿势
 4 '武藤兰'
 5 >>> info
 6 {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
 7 >>> del info['stu1103'] #换个姿势删除
 8 >>> info
 9 {'stu1102': 'LongZe Luola'}
10 >>> 
11 >>> 
12 >>> 
13 >>> info = {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
14 >>> info
15 {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} #随机删除
16 >>> info.popitem()
17 ('stu1102', 'LongZe Luola')
18 >>> info
19 {'stu1103': 'XiaoZe Maliya'}

 

4.查找

 1 >>> info = {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
 2 >>> 
 3 >>> "stu1102" in info #标准用法
 4 True
 5 >>> info.get("stu1102")  #获取
 6 'LongZe Luola'
 7 >>> info["stu1102"] #同上,但是看下面
 8 'LongZe Luola'
 9 >>> info["stu1105"]  #如果一个key不存在,就报错,get不会,不存在只返回None
10 Traceback (most recent call last):
11   File "<stdin>", line 1, in <module>
12 KeyError: 'stu1105'

 

5.嵌套

 1 av_catalog = {
 2     "欧美":{
 3         "www.youporn.com": ["很多免费的,世界最大的","质量一般"],
 4         "www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"],
 5         "letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"],
 6         "x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"]
 7     },
 8     "日韩":{
 9         "tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"]
10     },
11     "大陆":{
12         "1024":["全部免费,真好,好人一生平安","服务器在国外,慢"]
13     }
14 }
15 
16 av_catalog["大陆"]["1024"][1] += ",可以用爬虫爬下来"
17 print(av_catalog["大陆"]["1024"])
18 #ouput 
19 ['全部免费,真好,好人一生平安', '服务器在国外,慢,可以用爬虫爬下来']

 

6.其他

 1 #values
 2 >>> info.values()
 3 dict_values(['LongZe Luola', 'XiaoZe Maliya'])
 4 
 5 #keys
 6 >>> info.keys()
 7 dict_keys(['stu1102', 'stu1103'])
 8 
 9 
10 #setdefault
11 >>> info.setdefault("stu1106","Alex")
12 'Alex'
13 >>> info
14 {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
15 >>> info.setdefault("stu1102","龙泽萝拉")
16 'LongZe Luola'
17 >>> info
18 {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
19 
20 
21 #update 
22 >>> info
23 {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
24 >>> b = {1:2,3:4, "stu1102":"龙泽萝拉"}
25 >>> info.update(b)
26 >>> info
27 {'stu1102': '龙泽萝拉', 1: 2, 3: 4, 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
28 
29 #items
30 info.items()
31 dict_items([('stu1102', '龙泽萝拉'), (1, 2), (3, 4), ('stu1103', 'XiaoZe Maliya'), ('stu1106', 'Alex')])
32 
33 
34 #通过一个列表生成默认dict,有个没办法解释的坑,少用吧这个
35 >>> dict.fromkeys([1,2,3],'testd')
36 {1: 'testd', 2: 'testd', 3: 'testd'}

 

7.循环

1 #方法1
2 for key in info:
3     print(key,info[key])
4 
5 #方法2
6 for k,v in info.items(): #会先把dict转成list,数据里大时莫用
7     print(k,v)

 

posted @ 2016-10-24 15:23  wangmo  阅读(265)  评论(0编辑  收藏  举报