dict["w"]="watermelon"
del(dict["a"])
dict["g"]="grapefruit"
print dict.pop("b")
print dict
dict.clear()
print dict
#字典的遍历
dict={"a":"apple","b":"banana","g":"grape","o":"orange"}
for k in dict:
print"dict[%s]="% k,dict[k]
#字典items()的使用
dict={"a":"apple","b":"banana","c":"grape","d":"orange"}
#每个元素是一个key和value组成的元组,以列表的方式输出
print dict.items()
#调用items()实现字典的遍历
dict={"a":"apple","b":"banana","g":"grape","o":"orange"}
for(k,v) in dict.items():
print"dict[%s]="% k,v
#调用iteritems()实现字典的遍历
dict={"a":"apple","b":"banana","c":"grape","d":"orange"}
print dict.iteritems()
for k,v in dict.iteritems():
print"dict[%s]="% k,v
for (k,v) in zip (dict.iterkeys(),dict.iteritems()):
print"dict[%s]="%k,v
#使用列表、字典作为字典的值
dict={"a":("apple",),"bo":{"b":"banana","o":"orange"},"g":["grape","grapegruit"]}
print dict["a"]
print dict["a"][0]
print dict["bo"]
print dict["bo"]["o"]
print dict["g"]
print dict["g"][1]
===================== RESTART: C:/Python27/dict,list.py =====================
banana
{'w': 'watermelon', 'o': 'orange', 'g': 'grapefruit'}
{}
dict[a]= apple
dict[b]= banana
dict[o]= orange
dict[g]= grape
[('a', 'apple'), ('c', 'grape'), ('b', 'banana'), ('d', 'orange')]
dict[a]= apple
dict[b]= banana
dict[o]= orange
dict[g]= grape
<dictionary-itemiterator object at 0x02D10720>
dict[a]= apple
dict[c]= grape
dict[b]= banana
dict[d]= orange
dict[a]= ('a', 'apple')
dict[c]= ('c', 'grape')
dict[b]= ('b', 'banana')
dict[d]= ('d', 'orange')
('apple',)
apple
{'b': 'banana', 'o': 'orange'}
orange
['grape', 'grapegruit']
grapegruit