码迷,mamicode.com
首页 > 编程语言 > 详细

python学习笔记(十四)之字典

时间:2017-10-31 23:50:00      阅读:361      评论:0      收藏:0      [点我收藏+]

标签:date()   das   none   style   索引   改变   判断   通过   技术   

字典:是python中唯一的映射类型,字典中每一项都是由键-值对组成的项。字典中没有索引,只有键和值。键的类型可以是整型,变量或字符串。

创建和访问字典:

技术分享
 1 >>> dict1 = {Lining:Anything is possible, Nike:Just do is, Adidas:Impossible is nothing}
 2 >>> for i in dict1:
 3 ...     print(i)
 4 ... 
 5 Lining
 6 Adidas
 7 Nike
 8 >>> for i in dict1:
 9 ...     print(dict1[i])
10 ... 
11 Anything is possible
12 Impossible is nothing
13 Just do is
14 >>> dict2 = {1:one,2:two,3:three}
15 >>> dict2[3]
16 three
17 >>> dict2[1]
18 one
19 >>> dict2[0]
20 Traceback (most recent call last):
21   File "<stdin>", line 1, in <module>
22 KeyError: 0
23 >>> dict3 = dict(((a,97),(b,98),(c,99)))
24 >>> dict3
25 {c: 99, b: 98, a: 97}
26 >>> dict4 = dict(Jobs = stay hungry, stay foolish, Agan = run,run,run)
27 >>> dict4
28 {Agan: run,run,run, Jobs: stay hungry, stay foolish}
View Code

通过键可以访问和修改对应的值,若对一个不存在的键进行赋值,会创建一个新项。

技术分享
1 >>> dict1
2 {Lining: Anything is possible, Adidas: Impossible is nothing, Nike: Just do is}
3 >>> dict1[Lining] = nothing is nothing
4 >>> dict1
5 {Lining: nothing is nothing, Adidas: Impossible is nothing, Nike: Just do is}
6 >>> dict1[nothing] = nothing is nothing
7 >>> dict1
8 {Lining: nothing is nothing, Adidas: Impossible is nothing, Nike: Just do is, nothing: nothing is nothing}
View Code

fromkeys()方法

dict.fromkeys(s[, v])  用s中的值作为键,v作为对应的值创建一个新的字典。

技术分享
1 >>> dict1 = {}
2 >>> dict1
3 {}
4 >>> dict1.fromkeys([1,2,3],(1,2,3))
5 {1: (1, 2, 3), 2: (1, 2, 3), 3: (1, 2, 3)}
6 >>> dict1.fromkeys([1,2,3],number)
7 {1: number, 2: number, 3: number}
View Code

keys() 返回字典键的引用

values() 返回字典的值的引用

items() 返回对应项的引用

技术分享
 1 >>> dict1 = dict1.fromkeys(range(32), )
 2 >>> dict1
 3 {0: , 1: , 2: , 3: , 4: , 5: , 6: , 7: , 8: , 9: , 10: , 11: , 12: , 13: , 14: , 15: , 16: , 17: , 18: , 19: , 20: , 21: , 22: , 23: , 24: , 25: , 26: , 27: , 28: , 29: , 30: , 31: }
 4 >>> for each in dict1.keys():
 5 ...     print(each, end =  )
 6 ... 
 7 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 >>> 
 8 >>> for each in dict1.values():
 9 ...     print(each, end =  )
10 ... 
11 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 >>> 
12 >>> for each in dict1.items():
13 ...     print(each, end =  )
14 ... 
15 (0, ) (1, ) (2, ) (3, ) (4, ) (5, ) (6, ) (7, ) (8, ) (9, ) (10, ) (11, ) (12, ) (13, ) (14, ) (15, ) (16, ) (17, ) (18, ) (19, ) (20, ) (21, ) (22, ) (23, ) (24, ) (25, ) (26, ) (27, ) (28, ) (29, ) (30, ) (31, ) >>> 
16 >>> dict1.keys()
17 dict_keys([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
18 >>> dict1.values()
19 dict_values([, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ])
20 >>> dict1.items()
21 dict_items([(0, ), (1, ), (2, ), (3, ), (4, ), (5, ), (6, ), (7, ), (8, ), (9, ), (10, ), (11, ), (12, ), (13, ), (14, ), (15, ), (16, ), (17, ), (18, ), (19, ), (20, ), (21, ), (22, ), (23, ), (24, ), (25, ), (26, ), (27, ), (28, ), (29, ), (30, ), (31, )])
22 >>> 
View Code

 使用不存在的键访问一个字典时,会引发一个KeyError异常。

>>> dict1[32]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 32

这时,可以使用get方法。

>>> dict1.get(32)
>>> dict1.get(32,‘no exist‘)
‘no exist‘

可以通过成员关系操作符(in,not in)来判断一个键是否在字典中

>>> 31 in dict1
True
>>> 32 in dict2
False

字典的成员操作比序列更加高效,序列时通过不断迭代来判断的,而字典时基于hash的查找。

 

清空字典:clear()

>>> dict1
{0: ‘赞‘, 1: ‘赞‘, 2: ‘赞‘, 3: ‘赞‘, 4: ‘赞‘, 5: ‘赞‘, 6: ‘赞‘, 7: ‘赞‘, 8: ‘赞‘, 9: ‘赞‘, 10: ‘赞‘, 11: ‘赞‘, 12: ‘赞‘, 13: ‘赞‘, 14: ‘赞‘, 15: ‘赞‘, 16: ‘赞‘, 17: ‘赞‘, 18: ‘赞‘, 19: ‘赞‘, 20: ‘赞‘, 21: ‘赞‘, 22: ‘赞‘, 23: ‘赞‘, 24: ‘赞‘, 25: ‘赞‘, 26: ‘赞‘, 27: ‘赞‘, 28: ‘赞‘, 29: ‘赞‘, 30: ‘赞‘, 31: ‘赞‘}
>>> dict2 = dict1
>>> dict1 = {}
>>> dict1
{}
>>> dict2
{0: ‘赞‘, 1: ‘赞‘, 2: ‘赞‘, 3: ‘赞‘, 4: ‘赞‘, 5: ‘赞‘, 6: ‘赞‘, 7: ‘赞‘, 8: ‘赞‘, 9: ‘赞‘, 10: ‘赞‘, 11: ‘赞‘, 12: ‘赞‘, 13: ‘赞‘, 14: ‘赞‘, 15: ‘赞‘, 16: ‘赞‘, 17: ‘赞‘, 18: ‘赞‘, 19: ‘赞‘, 20: ‘赞‘, 21: ‘赞‘, 22: ‘赞‘, 23: ‘赞‘, 24: ‘赞‘, 25: ‘赞‘, 26: ‘赞‘, 27: ‘赞‘, 28: ‘赞‘, 29: ‘赞‘, 30: ‘赞‘, 31: ‘赞‘}
>>> dict1 = dict2
>>> dict1.clear()
>>> dict1
{}
>>> dict2
{}

字典拷贝:copy()

  这是浅拷贝,和直接赋值不同。浅拷贝,深拷贝都是对一个对象的浅层或深层的拷贝,但是,直接赋值则只是多了一个别名。

>>> a = {1:‘one‘,2:‘two‘,3:‘three‘}
>>> b = a.copy()
>>> c = a

  a,c是同一个对象的两个不同的名字,修改其中任意一个都会是另一个的值发生改变,因为本质上修改对方和修改自己是一样的。而b则是另一个不同的对象,对a或c作任何改变都不会印象b,反过来亦是如此。

pop() 给出键,弹出值

popitem() 随机弹出一个项

>>> a.pop(2)
‘two‘
>>> c
{1: ‘one‘, 3: ‘three‘}
>>> c.popitem()
(1, ‘one‘)
>>> b
{1: ‘one‘, 2: ‘two‘, 3: ‘three‘}

setdefault() 和get类似,但找不到键时进行添加

>>> a.setdefault(‘white‘)
>>> a
{‘white‘: None, 3: ‘three‘}
>>> a.setdefault(‘white‘,5)
>>> a
{‘white‘: None, 3: ‘three‘}
>>> a.setdefault(‘black‘,5)
5
>>> a
{‘white‘: None, ‘black‘: 5, 3: ‘three‘}
>>> a.setdefault(‘black‘,5)
5
>>> a.setdefault(‘black‘,6)
5
>>> a
{‘white‘: None, ‘black‘: 5, 3: ‘three‘}

update() 利用一个字典或映射关系去更新一个字典

>>> a
{‘white‘: None, ‘black‘: 5, 3: ‘three‘}
>>> b
{1: ‘one‘, 2: ‘two‘, 3: ‘three‘}
>>> a.update(b)
>>> a
{1: ‘one‘, 2: ‘two‘, 3: ‘three‘, ‘white‘: None, ‘black‘: 5}

 

python学习笔记(十四)之字典

标签:date()   das   none   style   索引   改变   判断   通过   技术   

原文地址:http://www.cnblogs.com/ZGreMount/p/7764010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!