Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1781317
  • 博文数量: 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 13:48:42

使用函数

点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. #函数与处理的数据打包一起.
  4. def filetolist(file,listname):
  5.     try:
  6.         #打开文件
  7.         with open(file) as jaf:
  8.             #读取数据行
  9.             data = jaf.readline()
  10.         #转换成list
  11.         listname=data.strip().split(',')
  12.         data = {}
  13.         data['name'] = listname.pop(0)
  14.         data['dob'] = listname.pop(0)
  15.         data['time'] = listname
  16.         result = print(data['name'] + '的三次最佳成绩是' + str(sorted(set([sanitize(each_it) for each_it in data['time']]))[0:3]))
  17.         #return listname
  18.         return result
  19.     except IOError as ioerr:
  20.         print('File error : %s' % ioerr)
  21.         return(None)
  22.         
  23. #处理字符,转换成m.s格式
  24. def sanitize(time_string):
  25.     if '-' in time_string:
  26.         splitter = '-'
  27.     elif ':' in time_string:
  28.         splitter = ':'
  29.     else:
  30.         return time_string
  31.     (min, sec) = time_string.split(splitter)
  32.     return (min + '.' + sec)

  33. for name in ["james", "julie", "mikey", "sarah"]:
  34.     thelist=filetolist(name+".txt",name)
  35.     #使用列表
  36.     #username=name+'user'
  37.     #userdob =name+'dob'
  38.     #username = thelist.pop(0)
  39.     #userdob = thelist.pop(0)
  40.     ##使用列表推导式
  41.     #name2 = [sanitize(each_it) for each_it in thelist]
  42.     ##使用工厂函数set()
  43.     #try:
  44.     # print(username + '的最佳成绩是' + str(sorted(set(name2))[0:3]))
  45.     #except TypeError as typerr:
  46.     # print('list type error %s' % typerr)
  47.     #使用字典
使用类

点击(此处)折叠或打开

  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.     def top3(self):
  10.         return(sorted(set([sanitize(time) for time in self.times]))[0:3])

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

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


t@localhost 6$ python3 kelly.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']
阅读(1332) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~