字典中的值没有特殊的顺序,但是都存储再一个特定的键里。键可以是数字,字符串甚至是元组。
1.字典创建和使用
>>> phonebook = {'Alice':'2345',"james":'3984','lucy':'7684',"Bruce":'0978','nancy':'00987'}
>>> phonebook['lucy']
'7684'
>>> phonebook['Bruce']
'0978'
感觉和jason的格式很像哈。
2.dict函数
>>> items = [('name','Bruce'),('tel','18081953671')]
>>> myinfo = dict(items)
>>> myinfo['name']
'Bruce'
>>> myinfo['tel']
'18081953671'
3.字典基本操作
>>> len(myinfo)
2
>>> myinfo['name']
'Bruce'
>>> myinfo['tel']='13880477963'
>>> myinfo['tel']
'13880477963'
>>> myinfo
{'name': 'Bruce', 'tel': '13880477963'}
>>> del myinfo['tel']
>>> myinfo
{'name': 'Bruce'}
>>> 'name' in myinfo
True
4.字典的格式化字符串
>>> phonebook={'Bruce':'888','nancy':'999','pipi':'098','all':'1000'}
>>> 'Bruce\'s phone number is %(Bruce)s.'%phonebook
"Bruce's phone number is 888."
5.字典方法
clear方法
>>> myinfo
{'name': 'Bruce', 'tel': '18081953671'}
>>> myinfo.clear()
>>> myinfo
{}
cpoy方法
>>> x={}
>>> y=x
>>> x['a']=77
>>> y
{'a': 77}
>>> y.clear()
>>> y
{}
>>> x
{}
------
>>> x['a'] = 77
>>> y=x.cpoy()
>>> y
{'a': 77}
>>> y.clear()
>>> y
{}
>>> x
{'a': 77}
------
>>> x= {'username':'admin','machines':['foo','bar','baz']}
>>> y=x.copy()
>>> y
{'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
>>> y['username']='mlh'
>>> y
{'username': 'mlh', 'machines': ['foo', 'bar', 'baz']}
>>> x
{'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
>>> y['machines'].remove('foo')
>>> y
{'username': 'mlh', 'machines': ['bar', 'baz']}
>>> x
{'username': 'admin', 'machines': ['bar', 'baz']}
copy 在副本中替换值的时候,原始字典不受影响。但如果副本修改了值,而不是替换,则原始字典也要受影响。
deepcopy方法
>>> from copy import deepcopy
>>> x
{'username': 'admin', 'machines': ['bar', 'baz']}
>>> y=deepcopy(x)
>>> y
{'username': 'admin', 'machines': ['bar', 'baz']}
>>> y['size'] = 38
>>> y
{'username': 'admin', 'machines': ['bar', 'baz'], 'size': 38}
>>> x
{'username': 'admin', 'machines': ['bar', 'baz']}
fromkeys方法
>>> x
{'username': 'admin', 'machines': ['bar', 'baz']}
>>> y=x.fromkeys('username','machines')
>>> y
{'u': 'machines', 's': 'machines', 'e': 'machines', 'r': 'machines', 'n': 'machines', 'a': 'machines', 'm': 'machines'}
>>> x
{'username': 'admin', 'machines': ['bar', 'baz']}
>>> y=x.fromkeys(['username','machines'])
>>> y
{'username': None, 'machines': None}
get方法
>>> y
{}
>>> y['name']
Traceback (most recent call last):
File "", line 1, in
y['name']
KeyError: 'name'
>>> print(y.get('name'))
None
has_key方法
items和iteritems方法(iteritems方法在3.0里面没有了)
>>> x.items()
dict_items([('username', 'admin'), ('machines', ['bar', 'baz'])])
>>> x = {'username': None, 'machines': None}
>>> it = x.iteritems()
>>> list(it)
[('username', None), ('machines', None)]
keys和iterkeys
pop方法
>>> x
{'username': 'admin', 'machines': ['bar', 'baz']}
>>> x.pop('username')
'admin'
>>> x
{'machines': ['bar', 'baz']}
popitem方法,随机弹出一个元素
setdefault方法
update方法
>>> x
{'machines': ['bar', 'baz']}
>>> name = {'name':'Bruce'}
>>> x.update(name)
>>> x
{'machines': ['bar', 'baz'], 'name': 'Bruce'}
values和itervalues(itervalues方法在3.0里面没有了)
>>> x
{'machines': ['bar', 'baz'], 'name': 'Bruce'}
>>> x.values()
dict_values([['bar', 'baz'], 'Bruce'])
小结:
本章主要有映射、利用字典格式化字符串、字典方法