跟老齐学Python之dict()的操作方法
2014-09-24 09:02
525 查看
dict的很多方法跟list有类似的地方,下面一一道来,并且会跟list做一个对比
嵌套
嵌套在list中也存在,就是元素是list,在dict中,也有类似的样式:
>>> a_list = [[1,2,3],[4,5],[6,7]] >>> a_list[1][1] 5 >>> a_dict = {1:{"name":"qiwsir"},2:"python","email":"qiwsir@gmail.com"} >>> a_dict {1: {'name': 'qiwsir'}, 2: 'python', 'email': 'qiwsir@gmail.com'} >>> a_dict[1]['name'] #一个嵌套的dict访问其值的方法:一层一层地写出键 'qiwsir'
获取键、值
在上一讲中,已经知道可以通过dict的键得到其值。例上面的例子。
还有别的方法得到键值吗?有!python一般不是只有一个方法实现某个操作的。
>>> website = {1:"google","second":"baidu",3:"facebook","twitter":4} >>>#用d.keys()的方法得到dict的所有键,结果是list >>> website.keys() [1, 'second', 3, 'twitter'] >>>#用d.values()的方法得到dict的所有值,如果里面没有嵌套别的dict,结果是list >>> website.values() ['google', 'baidu', 'facebook', 4] >>>#用items()的方法得到了一组一组的键值对, >>>#结果是list,只不过list里面的元素是元组 >>> website.items() [(1, 'google'), ('second', 'baidu'), (3, 'facebook'), ('twitter', 4)]
从上面的结果中,我们就可以看出,还可以用for语句循环得到相应内容。例如:
>>> for key in website.keys(): ... print key,type(key) ... 1 <type 'int'> second <type 'str'> 3 <type 'int'> twitter <type 'str'> >>>#下面的方法和上面的方法是一样的 >>> for key in website: ... print key,type(key) ... 1 <type 'int'> second <type 'str'> 3 <type 'int'> twitter <type 'str'>
以下两种方法等效:
>>> for value in website.values(): ... print value ... google baidu facebook 4 >>> for key in website: ... print website[key] ... google baidu facebook 4
下面的方法又是等效的:
>>> for k,v in website.items(): ... print str(k)+":"+str(v) ... 1:google second:baidu 3:facebook twitter:4 >>> for k in website: ... print str(k)+":"+str(website[k]) ... 1:google second:baidu 3:facebook twitter:4
下面的方法也能得到键值,不过似乎要多敲键盘
>>> website {1: 'google', 'second': 'baidu', 3: 'facebook', 'twitter': 4} >>> website.get(1) 'google' >>> website.get("second") 'baidu'
其它几种常用方法
dict中的方法在这里不做过多的介绍,因为前面一节中已经列出来类,看官如果有兴趣可以一一尝试。下面列出几种常用的
>>> len(website) 4 >>> website {1: 'google', 'second': 'baidu', 3: 'facebook', 'twitter': 4} >>> new_web = website.copy() #拷贝一份,这个拷贝也叫做浅拷贝,对应着还有深拷贝。 >>> new_web #两者区别,可以google一下。 {1: 'google', 'second': 'baidu', 3: 'facebook', 'twitter': 4}
删除键值对的方法有两个,但是两者有一点区别
>>>#d.pop(key),根据key删除相应的键值对,并返回该值 >>> new_web.pop('second') 'baidu' >>> del new_web[3] #没有返回值,如果删除键不存在,返回错误 >>> new_web {1: 'google', 'twitter': 4} >>> del new_web[9] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 9
用d.update(d2)可以把d2合并到d中。
>>> cnweb {'qq': 'first in cn', 'python': 'qiwsir.github.io', 'alibaba': 'Business'} >>> website {1: 'google', 'second': 'baidu', 3: 'facebook', 'twitter': 4} >>> website.update(cnweb) #把cnweb合并到website内 >>> website #变化了 {'qq': 'first in cn', 1: 'google', 'second': 'baidu', 3: 'facebook', 'python': 'qiwsir.github.io', 'twitter': 4, 'alibaba': 'Business'} >>> cnweb #not changed {'qq': 'first in cn', 'python': 'qiwsir.github.io', 'alibaba': 'Business'}
在本讲最后,要提醒看官,在python3中,dict有不少变化,比如能够进行字典解析,就类似列表解析那样,这可是非常有意思的东西哦。
您可能感兴趣的文章:
- Python 字典(Dictionary)操作详解
- python两种遍历字典(dict)的方法比较
- python通过字典dict判断指定键值是否存在的方法
- python 将字符串转换成字典dict
- Python中实现两个字典(dict)合并的方法
- python dict remove数组删除(del,pop)
- python 字典(dict)按键和值排序
- python list转dict示例分享
- 如何将python中的List转化成dictionary
- Python的“二维”字典 (two-dimension dictionary)定义与实现方法
- python中字典(Dictionary)用法实例详解
- Python中字典(dict)和列表(list)的排序方法实例
- Python中dictionary items()系列函数的用法实例
- python中defaultdict的用法详解
- python 字典(dict)遍历的四种方法性能测试报告
- python实现字典(dict)和字符串(string)的相互转换方法
- Python中内置数据类型list,tuple,dict,set的区别和用法
- python中dict字典的查询键值对 遍历 排序 创建 访问 更新 删除基础操作方法
相关文章推荐
- 跟老齐学Python之dict()的操作方法
- Python 学习笔记【dict的操作方法】
- Python的dict字典结构操作方法学习笔记
- Python 学习笔记【dict的操作方法】
- [零基础学Python]dict()的操作方法
- Python中xmltodict对xml的操作方法
- python中dict字典的查询键值对 遍历 排序 创建 访问 更新 删除基础操作方法
- Python的dict字典结构操作方法学习笔记
- Python列表(list)和字典(dict)的操作方法
- python中字典dict常用操作方法实例总结
- Python的dict字典结构操作方法学习笔记
- python的list,dict,tuple比较和应用
- Python-字符串操作方法(转)
- python基础31[list+tuple+set+dict+str+file的成员方法]
- C++ python 交互之 python.dict to c++ map
- python嵌入C++------ boost.python如何在C++中调用含有不定长参数tuple变量和关键字参数dict变量的函数
- python中将json字符串转换为dict
- Python从两个List构造Dict
- Python中的Dict对象(Python 源码剖析第五章)
- python中列表(list)和字典(dict)数据排序