字典,即我们可以通过索引来查找更详细的内容。每个item都是由一对index:value构成的
- 索引可以有副本,但是试图根据存在副本的索引访问元素时,只会取最靠后的那个。
- 索引必须是immutable的,意味着可以用string, number,tuple都可以,但是list不行
#创建字典
>>> dicA = { 'Num':1 , 'Grade':98 , 'Sch':yes }
#访问元素
>>> print dicA['Sch'], dicA['Num']
yes 1
#添加元素
>>> dicB = {'When':'Today' , 'Affair':'Meeting' , 'Who':'Tom'}
>>> dicB['WithWho'] = 'Sunny'
>>> print dicB
{'Affair': 'Meeting', 'Who': 'Tom', 'When': 'Today', 'WithWho': 'Sunny'}
#更新元素
>>> dicB['Who'] = 'XJ'
>>> print dicB
{'Affair': 'Meeting', 'Who': 'XJ', 'When': 'Today', 'WithWho': 'Sunny'}
#删除元素
>>> dicB = {'When':'Today' , 'Affair':Meeting , 'Who':Tom}
>>> del dicB['When']
>>> print dicB
{'Affair': 'Meeting', 'Who': 'Tom'}
#清空元素
>>> dicB.clear()
>>> print dicB
{}
#删除整个字典
>>> del dicB
>>> print dicB
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'dicB' is not defined Built-in Functions
cmp(dict1, dict2)
Compares elements of both dict.Returns 0 if both dictionaries are equal, -1 if dict1 < dict2 and 1 if dict1 > dic2.
>>> dicA,dicB
({'Num': 1, 'Sch': 'yes'}, {'Num': 2, 'Sch': 'yes'})
>>> del dicB['Num']
>>> cmp(dicA,dicB)
1
>>> dicA,dicB
({'Num': 1, 'Sch': 'yes'}, {'Sch': 'yes'})
>>> dicB['Num']=1
>>> cmp(dicA,dicB)
0 len(dict)
Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.
>>> len(dicA) 2
str(dict)
Produces a printable string representation of a dictionary
>>> str(dicA)
"{'Num': 1, 'Sch': 'yes'}"
>>> print str(dicA)
{'Num': 1, 'Sch': 'yes'} type(variable)
Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type
>>> print "%s" %type(dicA) <type 'dict'>
Built-in Methods
dict.clear()
Removes all elements of dictionary dict
>>> dicB.clear()
>>> print dicB
{} dict.copy()
Returns a shallow copy of dictionary dict. VS DicA = DicB means these two dictionaries share the same address, one is modified automaticaly with the other is changed
>>> dicB=dict.copy(dicA) #或dicB.copy(dicA)
>>> dicB['Num']=2
>>> dicA,dicB
({'Num': 1, 'Sch': 'yes'}, {'Num': 2, 'Sch': 'yes'}) dict.fromkeys()
Create a new dictionary with keys from seq and values set to value.
>>> dicE = {}
>>> dicE
{}
>>> seq = ('Who','When','What')
>>> dicE = dict.fromkeys(seq) #或dicE.fromkeys(seq)
>>> dicE
{'What': None, 'Who': None, 'When': None}
>>> dicE = dict.fromkeys(seq,'XXX')
>>> dicE
{'What': 'XXX', 'Who': 'XXX', 'When': 'XXX'} dict.get(key, default=None)
For key key, returns value or default if key not in dictionary
>>> dict.get(dicE,'Which') #或dicE.get('Which') >>> dict.get(dicE,'Who') 'XXX'
dict.has_key(key)
Returns true if key in dictionary dict, false otherwise
>>> dict.has_key(dicE,'Who') #或dicE.has_key('Who') True
dict.items()
Returns a list of dict's (key, value) tuple pairs
>>> dict.items(dicE) #或dictE.items() [('What', 'XXX'), ('Who', 'XXX'), ('When', 'XXX')]
dict.keys()
Returns list of dictionary dict's keys
>>> dict.keys(dicE) #或dicE.keys() ['What', 'Who', 'When']
dict.setdefault(key, default=None)
Similar to get(), but will set dict[key]=default if key is not already in dict
>>> dict.get(dicE,'Which') #或dicE.get('Which')
>>> dict.setdefault(dicE,'Which')
>>> dicE
{'What': 'XXX', 'Who': 'XXX', 'When': 'XXX', 'Which': None} dict.update(dict2)
Adds dictionary dict2's key-values pairs to dict
>>> dicF = {'Work':'No'}
>>> dicE
{'Num': 1, 'Sch': 'yes'}
>>> dicE.update(dicF)
>>> dicE
{'Num': 1, 'Sch': 'yes', 'Work': 'No'} dict.values()
Returns list of dictionary dict's values
>>> dicE.values() [1, 'yes', 'No']