相同功能,演进实现

数据文件

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- 返回dict

return({'Name':data_list.pop(0),

'DOB':data_list.pop(0),

'Time':str( sorted( set([sanitize(t) for t in data_list] ) )[0:3])})

 def sanitize(time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return (time_string) (mins, secs) = time_string.split(splitter)
return(mins + '.' + secs) def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline()
data_list = data.strip().split(',')
return({'Name':data_list.pop(0),
'DOB':data_list.pop(0),
'Time':str( sorted( set([sanitize(t) for t in data_list] ) )[0:3])})
except IOError as err:
print("file err:" + str(err))
return(none) julie = get_coach_data('julie2.txt')
james = get_coach_data('james2.txt')
sarah = get_coach_data('sarah2.txt')
mikey = get_coach_data('mikey2.txt') print( julie['Name'] + "'s faster time are:" + julie['Time'])
print( james['Name'] + "'s faster time are:" + james['Time'])
print( sarah['Name'] + "'s faster time are:" + sarah['Time'])
print( mikey['Name'] + "'s faster time are:" + mikey['Time'])

2- 返回类  return ( Athlete(datalist.pop(0), datalist.pop(0), datalist))

 def sanitize(time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return(time_string) (mins, secs) = time_string.split(splitter)
return(mins + "." + secs) class Athlete:
def __init__(self, a_name, a_dob = None, a_time = []):
self.name = a_name
self.dob = a_dob
self.times = a_time def top3(self):
return( sorted(set([sanitize(t) for t in self.times]))[0:3] ) def add_time(self,time_value):
self.times.append(time_value) def add_times(self,list_of_times):
self.times.extend(list_of_times) def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline();
datalist = data.strip().split(',')
return ( Athlete(datalist.pop(0), datalist.pop(0), datalist))
except IOError as err:
print('file err:' + str(err))
return(none) julie = get_coach_data('julie2.txt')
james = get_coach_data('james2.txt')
sarah = get_coach_data('sarah2.txt')
mikey = get_coach_data('mikey2.txt') print(julie.name + "'s faster times are:" + str(julie.top3()))
print(james.name + "'s faster times are:" + str(james.top3()))
print(sarah.name + "'s faster times are:" + str(sarah.top3()))
print(mikey.name + "'s faster times are:" + str(mikey.top3())) vera = Athlete('vera vi')
vera.add_time('1.30')
print(vera.top3())
vera.add_times(['1.00','0.3','1.2'])
print(vera.top3())

3- 继承python内置list

 class Athletelist(list):
def __init__(self, a_name, a_dob = None, a_times = []):
list.__init__([])
self.name = a_name
self.dob = a_dob
self.extend(a_times)
def top3(self):
return( sorted( set([sanitize(t) for t in self] ) )[0:3]) def sanitize(time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return(time_string) (mins, secs) = time_string.split(splitter)
return(mins + "." + secs) #vera = Athletelist("vera vi")
#vera.append('1.31')
#print(vera.top3())
#vera.extend(["1","2","0"])
#print(vera.top3()) def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline();
datalist = data.strip().split(',')
return ( Athletelist(datalist.pop(0), datalist.pop(0), datalist))
except IOError as err:
print('file err:' + str(err))
return(none) julie = get_coach_data('julie2.txt')
james = get_coach_data('james2.txt')
sarah = get_coach_data('sarah2.txt')
mikey = get_coach_data('mikey2.txt') print(julie.name + "'s faster times are:" + str(julie.top3()))
print(james.name + "'s faster times are:" + str(james.top3()))
print(sarah.name + "'s faster times are:" + str(sarah.top3()))
print(mikey.name + "'s faster times are:" + str(mikey.top3()))

最新文章

  1. NLP--十项沟通前的思想准备
  2. tyvj1008 传球游戏
  3. PHP空数组转化为json对象的问题
  4. DB2 错误信息码
  5. BOM与DOM
  6. TR069协议向导——一个帮助你了解TR069协议的简明教程(一)
  7. jquery easyui Tab 引入页面的问题
  8. Elasticsearch聚合——aggregation
  9. Jvm原理剖析与调优之内存结构
  10. 查看端口占用cmd命令
  11. VS遇到的问题记录
  12. python学习: 如何循序渐进学习Python语言
  13. java web启动后执行初始化任务
  14. 群晖Nas中搭建Intellij Idea的LicenseServer服务
  15. HTML5 -- 浏览器数据缓存 -- indexedDB
  16. codeforces 1077F1
  17. Jenkins自动化CI CD流水线之2--用户权限管理
  18. Vue nodejs商城项目-搭建express框架环境
  19. Android 找不到资源的问题
  20. python-pycharm windows安装

热门文章

  1. uva201 Squares
  2. win7 下的open live writer代码插件
  3. 在fedora 20下使用ssh server
  4. Windows 下 pip和easy_install 的安装与使用
  5. JSP SMARTUPLOAD组件:上传文件时同时获取表单参数
  6. Find the Celebrity 解答
  7. 关于qt学习的一点小记录(1)
  8. POJ3669(Meteor Shower)(bfs求最短路)
  9. 【POJ1003】Hangover(二分搜索)
  10. OpenMeetings(3)----启动顺序解析