Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1825039
  • 博文数量: 276
  • 博客积分: 1574
  • 博客等级: 上尉
  • 技术积分: 2894
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-26 23:23
个人简介

生活的美妙在于,不知道一下秒是惊艳还是伤神,时光流转,珍惜现在的拥有的时光

文章分类

全部博文(276)

文章存档

2017年(17)

2016年(131)

2015年(63)

2013年(2)

2012年(32)

2011年(31)

分类: Python/Ruby

2016-05-23 19:06:49


点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. import os
  4. class athlete:
  5.     def __init__(self, athlete_name, athlete_dob=None, athlete_times=[]):
  6.         self.name = athlete_name
  7.         self.dob = athlete_dob
  8.         self.times= athlete_times
  9.     #运动员最好的3组成绩
  10.     def top3(self):
  11.         return(sorted(set([sanitize(time) for time in self.times]))[0:3])
  12.     #为运动员添加一个成绩
  13.     def add_time(self, time_value):
  14.         self.times.append(time_value)
  15.     #为运动员添加一组成绩,使用列表类型.
  16.     def add_times(self, time_list):
  17.         self.times.extend(time_list)

  18. def openfile(filename):
  19.     try:
  20.         #打开文件
  21.         with open(filename) as athlete_file:
  22.             #读取数据
  23.             data = athlete_file.readline()
  24.             value_list= data.strip().split(',')
  25.             username = value_list.pop(0)
  26.             userdob = value_list.pop(0)
  27.             usertimes= value_list
  28.             #返回实例对象
  29.             athlete_instance=athlete(username,userdob,usertimes)
  30.             return(athlete_instance)
  31.     except IOError as ioerr:
  32.         print('File error %s' % ioerr)
  33.         return(None)

  34. #处理字符,转换成m.s格式
  35. def sanitize(time_string):
  36.     if '-' in time_string:
  37.         splitter = '-'
  38.     elif ':' in time_string:
  39.         splitter = ':'
  40.     else:
  41.         return time_string
  42.     (min, sec) = time_string.split(splitter)
  43.     return (min + '.' + sec)
  44. for name in ["james", "julie", "mikey", "sarah"]:
  45.     name = openfile(name+'.txt')
  46.     print(name.name + '的三次最佳成绩是' + str(name.top3()))


  47. talen = athlete('talen')
  48. talen.add_time('3.25')
  49. talen.add_time('3.45')
  50. talen.add_times(['1.30','2.59'])
  51. print(str(talen.top3()))

t@localhost 6$ python3 kelly_c.py
James Lee的三次最佳成绩是['2.01', '2.16', '2.22']
Julie Jones的三次最佳成绩是['2.11', '2.23', '2.59']
Mikey McManus的三次最佳成绩是['2.22', '2.31', '2.38']
Sarah Sweeney的三次最佳成绩是['2.18', '2.21', '2.22']
['1.30', '2.59', '3.25']

继承list类

点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. import os
  4. class athlete:
  5.     def __init__(self, athlete_name, athlete_dob=None, athlete_times=[]):
  6.         self.name = athlete_name
  7.         self.dob = athlete_dob
  8.         self.times= athlete_times
  9.     #运动员最好的3组成绩
  10.     def top3(self):
  11.         return(sorted(set([sanitize(time) for time in self.times]))[0:3])
  12.     #为运动员添加一个成绩
  13.     def add_time(self, time_value):
  14.         self.times.append(time_value)
  15.     #为运动员添加一组成绩,使用列表类型.
  16.     def add_times(self, time_list):
  17.         self.times.extend(time_list)
  18. #使用类继承,继承内置list类
  19. class athletelist(list):
  20.     def __init__(self, a_name, a_dob=None, a_times=[]):
  21.         list.__init__([])
  22.         self.name = a_name
  23.         self.dob = a_dob
  24.         self.extend(a_times)
  25.     def top3(self):
  26.         return(sorted(set([sanitize(t) for t in self]))[0:3])

  27. def openfile(filename):
  28.     try:
  29.         #打开文件
  30.         with open(filename) as athlete_file:
  31.             #读取数据
  32.             data = athlete_file.readline()
  33.             value_list= data.strip().split(',')
  34.             username = value_list.pop(0)
  35.             userdob = value_list.pop(0)
  36.             usertimes= value_list
  37.             #返回实例对象
  38.             athlete_instance=athlete(username,userdob,usertimes)
  39.             return(athlete_instance)
  40.     except IOError as ioerr:
  41.         print('File error %s' % ioerr)
  42.         return(None)

  43. #处理字符,转换成m.s格式
  44. def sanitize(time_string):
  45.     if '-' in time_string:
  46.         splitter = '-'
  47.     elif ':' in time_string:
  48.         splitter = ':'
  49.     else:
  50.         return time_string
  51.     (min, sec) = time_string.split(splitter)
  52.     return (min + '.' + sec)
  53. for name in ["james", "julie", "mikey", "sarah"]:
  54.     name = openfile(name+'.txt')
  55.     print(name.name + '的三次最佳成绩是' + str(name.top3()))


  56. talen = athlete('talen')
  57. talen.add_time('3.25')
  58. talen.add_time('3.45')
  59. talen.add_times(['1.30','2.59'])
  60. print(str(talen.top3()))
  61. ken = athletelist('ken')
  62. #为运动员添加一个成绩
  63. #由于继承list,不需要自己再定义添加方法,直接使用list的方法
  64. ken.append('4.25')
  65. #为运动员添加一组成绩,使用列表类型.
  66. ken.extend(['4.56','6.20','5.20'])
  67. print(ken.top3())


阅读(1230) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~