创建和访问字典
用大括号表示字典,字典是映射类型,(字典不是序列类型,字符串、列表、元组都是序列类型)
>>> dic1 = {'nike':'just do it','adidas':'impossible is nothing','lining':'一切皆有可能'}
索引的方式和列表、元组一致都是中括号,但是中括号内不是索引值,而是键值(key,可以是整形,字符)
>>> print('nike的slogan是:',dic1['nike'])
nike的slogan是: just do it
创建空的字典
>>> dict1 ={}
>>> dict1
{}
>>>
使用dict()工厂函数(类型)
直接使用元组或列表参数创建字典
>>> dict3 = dict((('F',70),('i',105),('s',115),('h',104),('c',67)))
>>> dict3
{'h': 104, 'i': 105, 'c': 67, 's': 115, 'F': 70}
>>>
使用key-value的形式创建字典
>>> dict4 = dict(nike='just do it',adidas='impossible is nothing')
>>> dict4
{'adidas': 'impossible is nothing', 'nike': 'just do it'}
>>>
但是使用key-value创建字典时,key是没有引号的
>>> dict4 = dict('nike'='just do it',adidas='impossible is nothing')
SyntaxError: keyword can't be an expression
>>>
因为关键字不能是表达式,dict会自动以字符串的形式把key包裹起来
直接给字典的键赋值
如果这个键(key)是存在的,就改变这个键对应的值。如果这个键是不存在的,则将这个键值对插入到字典中
>>> dict4
{'adidas': 'impossible is nothing', 'nike': 'just do it'}
>>> dict4['nike'] = '做就做'
>>> dict4
{'adidas': 'impossible is nothing', 'nike': '做就做'}
>>> dict4['lining'] = '一切皆有可能'
>>> dict4
{'lining': '一切皆有可能', 'adidas': 'impossible is nothing', 'nike': '做就做'}
>>>
字典的内建方法
创建字典的方法
dict.fromkeys()
>>> dict1 = {}
>>> dict.fromkeys((1,2,3))
{1: None, 2: None, 3: None}
>>> dict1.fromkeys((1,2,4),'number')
{1: 'number', 2: 'number', 4: 'number'}
>>> dict1.fromkeys((1,2,3),('one','two','three'))
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
>>>
访问字段的方法
dict.keys()
先创建多个键值对的字典
>>> dict1 = dict1.fromkeys(range(10),'good')
>>> dict1
{0: 'good', 1: 'good', 2: 'good', 3: 'good', 4: 'good', 5: 'good', 6: 'good', 7: 'good', 8: 'good', 9: 'good'}
>>> for eachKey in dict1.keys():
print(eachKey)
0
1
2
3
4
5
6
7
8
9
>>>
dict.values()
>>> for eachValue in dict1.values():
print(eachValue)
good
good
good
good
good
good
good
good
good
good
>>>
dict.items()
>>> for eachItem in dict1.items():
print(eachItem)
(0, 'good')
(1, 'good')
(2, 'good')
(3, 'good')
(4, 'good')
(5, 'good')
(6, 'good')
(7, 'good')
(8, 'good')
(9, 'good')
>>>
dict.get()
如果key存在,返回key对应的值。如果key不存在则返回默认值,若默认值没有给,则返回None
>>> dict1.get(0)
'good'
>>> dict1.get(10)
>>> print(dict1.get(10))
None
>>> dict1.get(10,'bad')
'bad'
检查键的成员资格
>>> 1 in dict1
True
>>> 10 in dict1
False
检查键的成员资格比序列更加高效,当数据非常大时,两者差距很大。(序列中查找的是元素值,而不是序列)
dict.clear()
>>> dict1.clear()
>>> dict1
{}
>>>
>>> a = {'nike':'usa'}
>>> b = a
>>> b
{'nike': 'usa'}
>>> a = {}
>>> a
{}
>>> b
{'nike': 'usa'}
>>> a = b
>>> a
{'nike': 'usa'}
>>> a.clear()
>>> a
{}
>>> b
{}
>>>
dict.copy()
全拷贝
>>> a = {1:'one',2:'two',3:'three'}
>>> b = a.copy()
>>> c = a
>>> a
{1: 'one', 2: 'two', 3: 'three'}
>>> b
{1: 'one', 2: 'two', 3: 'three'}
>>> c
{1: 'one', 2: 'two', 3: 'three'}
>>> id(a)
30397384
>>> id(b)
21226480
>>> id(c)
30397384
>>>
>>> c[4] = 'four'
>>> c
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
>>> a
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
>>> b
{1: 'one', 2: 'two', 3: 'three'}
>>>
dict.setdefault()
如果key在字典中,返回对应的值;如果不存在,则插入项,默认值为None
>>> a = {1:'one',2:'two',3:'three'}
>>> a.setdefault(1)
'one'
>>> a.setdefault(4)
>>> a
{1: 'one', 2: 'two', 3: 'three', 4: None}
>>> a.setdefault(5,'five')
'five'
>>> a
{1: 'one', 2: 'two', 3: 'three', 4: None, 5: 'five'}
>>>
dict.pop()
>>> a
{1: 'one', 2: 'two', 3: 'three', 4: None, 5: 'five'}
>>> a.pop(2)
'two'
>>> a
{1: 'one', 3: 'three', 4: None, 5: 'five'}
>>> a.pop(1,'one')
'one'
>>> a
{3: 'three', 4: None, 5: 'five'}
>>> a.pop(6)
Traceback (most recent call last):
File "<pyshell#90>", line 1, in <module>
a.pop(6)
KeyError: 6
>>>
dict.popitem()
>>> a
{3: 'three', 4: None, 5: 'five'}
>>> a.popitem()
(3, 'three')
>>> a
{4: None, 5: 'five'}
>>>
dict.update()
用一个字典或映射关系更新另一个字典
>>> a
{4: None, 5: 'five'}
>>> b = {4:'four'}
>>> a.update(b)
>>> a
{4: 'four', 5: 'five'}
>>> a.update({5:'FIVE'})
>>> a
{4: 'four', 5: 'FIVE'}
>>>