python 学习日记(五)

dictionary的应用

他就相当于java中的map等集合,可以设置key和value的值方便操作

定义时的方法:dictionary['name':'XXXX','second':'XXXX']

调用时的方法是:dictionary['name']

假设有如下文档

sarah2.txt 内容:Sarah Sweeney,2002-6-17,2.58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55

需要将她的名字及时间信息的前三位取出来,并且时间不能重复

 1 def sanitize(time_string):  #定义一个将时间格式统一成X.XX这样的形式方便后面的排序比较
 2     if ':' in time_string:
 3         splitter  = ':'
 4     elif '-' in time_string:
 5         splitter = '-'
 6     else:
 7         return time_string
 8     (mins,second) = time_string.split(splitter)
 9     return(mins + '.' + second)
10 
11 def getinfo(path):  #通过输入路径来得到文件中的内容
12     try:
13         with open(path) as file :
14             data = file.readline().strip().split(',')
15             info = {'name':data[0],'BOD':data[1],
16                           'time':str(sorted(set([sanitize(each) for each in data[2:]]))[0:3])}
          #time中的信息解释:从内到外。先将data中的每个成员统一格式,然后放到set中取出相同元素。再排序取得前三位。转换成字符串
17         #将文件中的信息按照name/BOD/time的格式写到dictionary中 18 return info 19 except IOError as err: 20 print('ioerr' + err) 21 return(None) 22 23 24 path = 'c:/Python33/source/sarah2.txt' 25 26 sarah = getinfo(path) 27 print(sarah['name']+ ':' + sarah['BOD'] + ',' + sarah['time']) #打印信息

使用类的方式解决上面的问题

注意点:声明类的时候def __init__():init的左右两边的下划线是由两根短的下划线组成的

定义方法时必须定义一个自身的参数self

 1 def sanitize(time_string):
 2     if ':' in time_string :
 3         splitter = ':'
 4     elif '-' in time_string:
 5         splitter = '-'
 6     else:
 7         return time_string
 8     (mins,second) = time_string.split(splitter)
 9     return(mins + '.' + second)
10 
11 def getinfo(path):
12     try:
13         with open(path) as file:
14             data = file.readline().strip().split(',')
15             return Athlete(data.pop(0),data.pop(0),data)
16     except IOError as err:
17         print('IOerr' + err)
18 
19 class Athlete:
20     def __init__(self,name,dob=None,times=[]):
21         self.name = name
22         self.dob = dob
23         self.times = times
24 
25     def getTop3(self):
26         return str(sorted(set([sanitize(each) for each in self.times]))[0:3])
27 
28 path = "c:/Python33/source/sarah2.txt"
29 
30 sarah = getinfo(path)
31 print(sarah.name + "times of top3 : " + sarah.getTop3())

Athlete的方法基本就是list的操作,不同点是增加了属性,在python中可以通过继承于list的类来实现相同的功能简化程序

 1 def sanitize(time_string):
 2     if ':' in time_string :
 3         splitter = ':'
 4     elif '-' in time_string:
 5         splitter = '-'
 6     else:
 7         return time_string
 8     (mins,second) = time_string.split(splitter)
 9     return(mins + '.' + second)
10 
11 def getinfo(path):
12     try:
13         with open(path) as file:
14             data = file.readline().strip().split(',')
15             return AthleteList(data.pop(0),data.pop(0),data)
16     except IOError as err:
17         print('IOerr' + err)
18 
19 class AthleteList(list):
20     def __init__(self,name,dob=None,times=[]):
21         list.__init__([])
22         self.name = name
23         self.dob = dob
24         self.extend(times)
25 
26     def getTop3(self):
27         return (sorted(set([sanitize(each) for each in self]))[0:3])
28         
29         
30 
31 path = "c:/Python33/source/sarah2.txt"
32 
33 sarah = getinfo(path)
34 
35 print(sarah.name + ': the top 3 first time is __ ' + str(sarah.getTop3()))

省去了处理athlete中增加和删除时间时需要的程序段

 

posted @ 2013-05-13 18:08  拙急鸟  阅读(210)  评论(0编辑  收藏  举报