head first python 6

简介: 使用函数 点击(此处)折叠或打开 #!/usr/bin/env python3 # -*- coding:utf-8 -*- #函数与处理的数据打包一起.
使用函数

点击(此处)折叠或打开

  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']
目录
相关文章
|
Web App开发 Python
Head First Python 7 web开发
t@localhost webapp$ tree . ├── cgi-bin │   ├── athletemodel.py │   ├── generate_list.py │   ├── generate_timing_data.
1163 0
|
Python
head first python 5
点击(此处)折叠或打开 #!/usr/bin/env python3 # -*- coding:utf-8 -*- def filetolist(file,listna...
671 0
|
Python
head first python 6 class 扩展
点击(此处)折叠或打开 #!/usr/bin/env python3 # -*- coding:utf-8 -*- import os clas...
841 0
|
7月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1120 102
|
7月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
445 104
|
7月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
350 103
|
7月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
324 82
|
6月前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
446 3
|
6月前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
654 3
|
6月前
|
并行计算 安全 计算机视觉
Python多进程编程:用multiprocessing突破GIL限制
Python中GIL限制多线程性能,尤其在CPU密集型任务中。`multiprocessing`模块通过创建独立进程,绕过GIL,实现真正的并行计算。它支持进程池、队列、管道、共享内存和同步机制,适用于科学计算、图像处理等场景。相比多线程,多进程更适合利用多核优势,虽有较高内存开销,但能显著提升性能。合理使用进程池与通信机制,可最大化效率。
462 3

推荐镜像

更多