字典的一个用法实例

2011-04-03  李琪 

#-*- coding:utf-8 -*-
'''
Created on 2011-4-3
作者: 李昱彤
Email: liqi1031@gmail.com
'''
def setDictProperty(phone='', age=0, sex=''):
    propertyDict={}
    propertyDict['phone']=phone
    propertyDict['age']=age
    propertyDict['sex']=sex
   
    return propertyDict
liqipropertyDict={}
wangjuanpropertyDict={}
liyutongpropertyDict={}
aDict={'liqi': liqipropertyDict, 'wangjuan': wangjuanpropertyDict, 'liyutong': liyutongpropertyDict}
if 'liqi' in aDict.keys():
    aDict['liqi']=setDictProperty('137xxxxx256', 31, 'male')
   
if 'wangjuan' in aDict.keys():
    aDict['wangjuan']=setDictProperty('132xxxxx935', 28, 'female')
   
if 'liyutong' in aDict.keys():
    aDict['liyutong']=setDictProperty(0, 1, 'female')
print aDict
for key in aDict.iterkeys():
    print '*' * 50
    for k in aDict[key].iterkeys():
        print "%s: %s" % (k, aDict[key][k])
 
运行结果:
{'liyutong': {'phone': 0, 'age': 1, 'sex': 'female'}, 'wangjuan': {'phone': '132xxxxx935', 'age': 28, 'sex': 'female'}, 'liqi': {'phone': '137xxxxx256', 'age': 31, 'sex': 'male'}}
**************************************************
phone: 0
age: 1
sex: female
**************************************************
phone: 132xxxxx935
age: 28
sex: female
**************************************************
phone: 137xxxxx256
age: 31
sex: male
315°/3156 人阅读/0 条评论 发表评论

登录 后发表评论