字典,即我们可以通过索引来查找更详细的内容。每个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 &#39;dicB&#39; 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
({&#39;Num&#39;: 1, &#39;Sch&#39;: &#39;yes&#39;}, {&#39;Num&#39;: 2, &#39;Sch&#39;: &#39;yes&#39;})
>>> del dicB[&#39;Num&#39;]
>>> cmp(dicA,dicB)
1
>>> dicA,dicB
({&#39;Num&#39;: 1, &#39;Sch&#39;: &#39;yes&#39;}, {&#39;Sch&#39;: &#39;yes&#39;})
>>> dicB[&#39;Num&#39;]=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)
"{&#39;Num&#39;: 1, &#39;Sch&#39;: &#39;yes&#39;}"
>>> print str(dicA)
{&#39;Num&#39;: 1, &#39;Sch&#39;: &#39;yes&#39;}

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 &#39;dict&#39;>

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[&#39;Num&#39;]=2
>>> dicA,dicB
({&#39;Num&#39;: 1, &#39;Sch&#39;: &#39;yes&#39;}, {&#39;Num&#39;: 2, &#39;Sch&#39;: &#39;yes&#39;})

dict.fromkeys()

Create a new dictionary with keys from seq and values set to value.

>>> dicE = {}
>>> dicE
{}
>>> seq = (&#39;Who&#39;,&#39;When&#39;,&#39;What&#39;)
>>> dicE = dict.fromkeys(seq)   #或dicE.fromkeys(seq)
>>> dicE
{&#39;What&#39;: None, &#39;Who&#39;: None, &#39;When&#39;: None}
>>> dicE = dict.fromkeys(seq,&#39;XXX&#39;)
>>> dicE
{&#39;What&#39;: &#39;XXX&#39;, &#39;Who&#39;: &#39;XXX&#39;, &#39;When&#39;: &#39;XXX&#39;}

dict.get(key, default=None)

For key key, returns value or default if key not in dictionary

>>> dict.get(dicE,&#39;Which&#39;)  #或dicE.get(&#39;Which&#39;)
>>> dict.get(dicE,&#39;Who&#39;)
&#39;XXX&#39;

dict.has_key(key)

Returns true if key in dictionary dict, false otherwise

>>> dict.has_key(dicE,&#39;Who&#39;)    #或dicE.has_key(&#39;Who&#39;)
True

dict.items()

Returns a list of dict's (key, value) tuple pairs

>>> dict.items(dicE)    #或dictE.items()
[(&#39;What&#39;, &#39;XXX&#39;), (&#39;Who&#39;, &#39;XXX&#39;), (&#39;When&#39;, &#39;XXX&#39;)]

dict.keys()

Returns list of dictionary dict's keys

>>> dict.keys(dicE) #或dicE.keys()
[&#39;What&#39;, &#39;Who&#39;, &#39;When&#39;]

dict.setdefault(key, default=None)

Similar to get(), but will set dict[key]=default if key is not already in dict

>>> dict.get(dicE,&#39;Which&#39;)  #或dicE.get(&#39;Which&#39;)
>>> dict.setdefault(dicE,&#39;Which&#39;)
>>> dicE
{&#39;What&#39;: &#39;XXX&#39;, &#39;Who&#39;: &#39;XXX&#39;, &#39;When&#39;: &#39;XXX&#39;, &#39;Which&#39;: None}

dict.update(dict2)

Adds dictionary dict2's key-values pairs to dict

>>> dicF = {&#39;Work&#39;:&#39;No&#39;}
>>> dicE
{&#39;Num&#39;: 1, &#39;Sch&#39;: &#39;yes&#39;}
>>> dicE.update(dicF)
>>> dicE
{&#39;Num&#39;: 1, &#39;Sch&#39;: &#39;yes&#39;, &#39;Work&#39;: &#39;No&#39;}

dict.values()

Returns list of dictionary dict's values

>>> dicE.values()
[1, &#39;yes&#39;, &#39;No&#39;]