Mapping a dictionary:

D = {'food': 'Spam', 'quantity':4}    #build with {}
D = dict(food='Spam', quantity=4)    #build with dict()

D={}
D['key'] = 'value'    #add key,value to a dict.

#Sorting Keys:

for key in sorted(D):
	print(key, '=>', D[key])

#Missing Keys:
value = D.get('x', 0)	#0 for default if None
value= D['key'] if 'x' in D else 0
posted on 2011-10-28 17:17  Aeol Kong  阅读(207)  评论(0)    收藏  举报