Python基础【day03】:字典(一)
本节内容
- 简述
- 语法
- 字典的使用
- 字典内置方法
一、简述
字典是Python中的又一种数据结构,它是通过key-value的数据类型,跟我们小时候用的新华字典差不多,通过key去访问value
二、语法
stu_info = { "xiedi":28, "liuhailin":27, "daiqiao":30, "hanwenhai":25, "chenqun":38 }
2、特性
①字典是无序的
②字典是通过key去访问value(元素不存在重复)
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38} #问通过key值访问对应的元素
>>> stu_info["chenqun"] 38 >>>
三、字典的使用
1、增加
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38}
>>> stu_info["chenqun"] 38
#新增 >>> stu_info["luoahong"]=32 >>> stu_info {'hanwenhai': 25, 'daiqiao': 30, 'luoahong': 32, 'chenqun': 38, 'xiedi': 28, 'liuhailin': 27}
2、修改
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38} #修改"xiedi"对应的value值,由28改为29
>>> stu_info["xiedi"]=29 #输出
>>> stu_info {'hanwenhai': 25, 'daiqiao': 30, 'chenqun': 38, 'xiedi': 29, 'liuhailin': 27}
3、删除(del、pop、popitem)
del
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38} >>> del stu_info["chenqun"] #结果输出
>>> stu_info {'hanwenhai': 25, 'daiqiao': 30, 'xiedi': 29, 'liuhailin': 27}
特别提醒:如果是del stu_info的话,则是删除stu_info这个变量
pop(k)
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38} #删除时给出提示
>>> stu_info.pop("liuhailin") 27 #输出结果
>>> stu_info {'hanwenhai': 25, 'daiqiao': 30, 'chenqun': 38, 'xiedi': 28}
popitem()
随机删除字典中的一个元素
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38} #随机删除的元素
>>> stu_info.popitem() ('hanwenhai', 25) #输出结果
>>> stu_info {'daiqiao': 30, 'chenqun': 38, 'xiedi': 28, 'liuhailin': 27}
4、查找
key值在字典中存在
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38} >>> "chenqun" in stu_info True >>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38} >>> stu_info.has_key("daiqiao") ##标准用法,在Python3和Python2.7都可以用 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'dict' object has no attribute 'has_key'
以上这种用法,在Python3和Python2.7都可以用,但是下面这种情况只能在Python2.7上用,Python3已经废除了
get(k)
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38} #存在则返回对应的value
>>> print(stu_info.get("daiqiao")) 30 >>> print(stu_info.get("hanwenhai")) 25
通过k获取value
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38} #存在key返回value
>>> stu_info["liuhailin"] 27 #不存在key提示报错
>>> stu_info["luoahong"] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'luoahong'
小结:get(k)不存在key值,则返回None,通过key直接访问会报错,所以建议推荐用get(k)这个方法获取value
5、多级字典嵌套及操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
av_catalog = { "欧美" :{ "www.youporn.com" : [ "很多免费的,世界最大的" , "质量一般" ], "www.pornhub.com" : [ "很多免费的,也很大" , "质量比yourporn高点" ], "letmedothistoyou.com" : [ "多是自拍,高质量图片很多" , "资源不多,更新慢" ], "x-art.com" :[ "质量很高,真的很高" , "全部收费,屌比请绕过" ] }, "日韩" :{ "tokyo-hot" :[ "质量怎样不清楚,个人已经不喜欢日韩范了" , "听说是收费的" ] }, "大陆" :{ "1024" :[ "全部免费,真好,好人一生平安" , "服务器在国外,慢" ] } } av_catalog[ "大陆" ][ "1024" ][ 1 ] + = ",可以用爬虫爬下来" print (av_catalog[ "大陆" ][ "1024" ]) #ouput [ '全部免费,真好,好人一生平安' , '服务器在国外,慢,可以用爬虫爬下来' ] |
四、字典的内容之方法
1、values()
返回字典中所有value,生成一个列表
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38} >>> stu_info.values()
#输出 dict_values([25, 30, 38, 28, 27]) >>>
2、keys()
返回字典中所有key,生成一个列表
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38}
>>> stu_info.keys()
#输出结果 dict_keys(['hanwenhai', 'daiqiao', 'chenqun', 'xiedi', 'liuhailin'])
3、setdefault(k,v)
setdefault()表示去取字典中的key,如果取不到,则设置新值,相反如果取到,则返回原有默认值
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38}
#设置不存在的key
>>> stu_info.setdefault("luoahong",32) 32 >>> stu_info #输出结果
{'hanwenhai': 25, 'daiqiao': 30, 'luoahong': 32, 'chenqun': 38, 'xiedi': 28, 'liuhailin': 27} #设置存在的key
>>> stu_info.setdefault("chenqun",28) 38 #输出结果
>>> stu_info {'hanwenhai': 25, 'daiqiao': 30, 'luoahong': 32, 'chenqun': 38, 'xiedi': 28, 'liuhailin': 27} >>>
4、update(dict)
update()是把两个字典合并成一个新的字典,中间有交叉的key,更新替换成新值,没有交叉就直接创建
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38} >>> yhd={"luoahong":32,1:2,3:4} >>> stu_info.update(yhd) >>> stu_info
#输出 {1: 2, 3: 4, 'chenqun': 38, 'xiedi': 28, 'hanwenhai': 25, 'daiqiao': 30, 'luoahong': 32, 'liuhailin': 27}
注:以后提到更新(update),就是直接原有的字典或者列表上修改,添加(上面的例子就是在stu_info基础上更新的)
5、items()
把字典转换成列表
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38} >>> stu_info.items() dict_items([('hanwenhai', 25), ('daiqiao', 30), ('chenqun', 38), ('xiedi', 28), ('liuhailin', 27)])
6、fromkeys(list,默认值)
初始化一个字典
>>> dict.fromkeys(["xiedi","liuhailin"],1) {'xiedi': 1, 'liuhailin': 1}
特别提醒:创建多层会有浅copy的作用,所以不建议使用,只需要做了解即可
7、clear()
清空字典
>>> stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38} >>> stu_info.clear() >>> stu_info {}
作者:罗阿红
出处:http://www.cnblogs.com/luoahong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。