1、区分不同类别的数据:pop(0)的调用将删除并返回列表最前面的数据项
>>> sarah=wbwj.dk('sarah2.txt')
>>> (sarah_name,sarah_dob)=sarah.pop(0),sarah.pop(0)
>>> print(str(sorted(set([zh.sanitize(t) for t in sarah]))[0:3]))
['2.18', '2.21', '2.22']
2、两种创建空字典的技术
>>> cleese={}
>>> palin=dict()
3、将值与键关联,向这两个字典分别增加一些数据。注意palin字典是一次性同时创建的。数据与键关联后,可以访问单个数据项。同时可以动态扩展存储额外的键/值。欲裂标不同,Python字典不会维持插入的顺序,对于字典,重点是关联关系,而不是顺序。
>>> cleese['Name']='John Cleese'
>>> cleese['Occupations']=['actor','comedian','writer','film producer']
>>> palin={'Name':'Michael Palin', 'Occupations':['comedian','actor','writer','tv']}
>>> palin['Name']
'Michael Palin'
>>> cleese['Occupation'][-1]
'film producer'
4、利用字典改变上节的练习
>>> sarah=wbwj.dk('sarah2.txt')
>>> sarah_data={}
>>> sarah_data['Name']=sarah.pop(0)
>>> sarah_data['DOB']=sarah.pop(0)
>>> sarah_data['Times']=sarah
>>> print(sarah_data['Name']+"'s fastest times are: "+str(sorted(set([zh.sanitize(t) for t in sarah_data['Times']]))[0:3]))
Sarah Sweeney's fastest times are: ['2.18', '2.21', '2.22']