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

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

相关文章

Python使用循环神经网络解决文本分类问题的方法详解

Python使用循环神经网络解决文本分类问题的方法详解

本文实例讲述了Python使用循环神经网络解决文本分类问题的方法。分享给大家供大家参考,具体如下: 1、概念 1.1、循环神经网络 循环神经网络(Recurrent Neural Net...

python 判断自定义对象类型

要判断自定义对象的类型,用__class__方法,或者用isinstance(object, class-or-type-or-tuple)-->bool 用__class__不能...

在Python中表示一个对象的方法

在 Python 中一切都是对象。如果要在 Python 中表示一个对象,除了定义 class 外还有哪些方式呢?我们今天就来盘点一下。 0x00 dict...

Python 使用with上下文实现计时功能

引言 with 语句是从 Python 2.5 开始引入的一种与异常处理相关的功能(2.5 版本中要通过 from __future__ import with_statement 导...

对Python3使运行暂停的方法详解

对Python3使运行暂停的方法详解

在Python3中已经有很大一部分语句与Python2不互通了,运行暂停的方法也有所不同。 1、input(); 这种方法不用包含模块,因此这也是最常用的一种暂停手段。 Python2中...