跟老齐学Python之dict()的操作方法

yipeiwu_com6年前Python基础

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有不少变化,比如能够进行字典解析,就类似列表解析那样,这可是非常有意思的东西哦。

相关文章

pytorch numpy list类型之间的相互转换实例

如下所示: import torch from torch.autograd import Variable import numpy as np ''' pytorch中Varia...

Python操作MySQL数据库的三种方法总结

Python操作MySQL数据库的三种方法总结

1. MySQLdb 的使用 (1) 什么是MySQLdb? MySQLdb 是用于 Python 连接 MySQL 数据库的接口,它实现了 Python 数据库 API 规范 V2.0...

win10环境下python3.5安装步骤图文教程

win10环境下python3.5安装步骤图文教程

点我去Python官网下载 往下翻几页就能看到各种版本的Python,当前最新的是Python3.6,也没多大区别,我选择的是3.5.2 64位的,点击download 根据自己的电...

python3下pygame如何实现显示中文

python3下pygame如何实现显示中文

这篇文章主要介绍了python3下pygame如何实现显示中文,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.先看代码: im...

python贪吃蛇游戏代码

python贪吃蛇游戏代码

本文实例为大家分享了python贪吃蛇游戏的具体代码,供大家参考,具体内容如下 贪吃蛇游戏截图: 首先安装pygame,可以使用pip安装pygame: pip install py...