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.
1007 0
|
Python
head first python 5
点击(此处)折叠或打开 #!/usr/bin/env python3 # -*- coding:utf-8 -*- def filetolist(file,listna...
615 0
|
Python
head first python 6 class 扩展
点击(此处)折叠或打开 #!/usr/bin/env python3 # -*- coding:utf-8 -*- import os clas...
760 0
|
3月前
|
人工智能 数据可视化 数据挖掘
探索Python编程:从基础到高级
在这篇文章中,我们将一起深入探索Python编程的世界。无论你是初学者还是有经验的程序员,都可以从中获得新的知识和技能。我们将从Python的基础语法开始,然后逐步过渡到更复杂的主题,如面向对象编程、异常处理和模块使用。最后,我们将通过一些实际的代码示例,来展示如何应用这些知识解决实际问题。让我们一起开启Python编程的旅程吧!
|
3月前
|
存储 数据采集 人工智能
Python编程入门:从零基础到实战应用
本文是一篇面向初学者的Python编程教程,旨在帮助读者从零开始学习Python编程语言。文章首先介绍了Python的基本概念和特点,然后通过一个简单的例子展示了如何编写Python代码。接下来,文章详细介绍了Python的数据类型、变量、运算符、控制结构、函数等基本语法知识。最后,文章通过一个实战项目——制作一个简单的计算器程序,帮助读者巩固所学知识并提高编程技能。
|
3月前
|
Unix Linux 程序员
[oeasy]python053_学编程为什么从hello_world_开始
视频介绍了“Hello World”程序的由来及其在编程中的重要性。从贝尔实验室诞生的Unix系统和C语言说起,讲述了“Hello World”作为经典示例的起源和流传过程。文章还探讨了C语言对其他编程语言的影响,以及它在系统编程中的地位。最后总结了“Hello World”、print、小括号和双引号等编程概念的来源。
128 80
|
2月前
|
存储 缓存 Java
Python高性能编程:五种核心优化技术的原理与Python代码
Python在高性能应用场景中常因执行速度不及C、C++等编译型语言而受质疑,但通过合理利用标准库的优化特性,如`__slots__`机制、列表推导式、`@lru_cache`装饰器和生成器等,可以显著提升代码效率。本文详细介绍了这些实用的性能优化技术,帮助开发者在不牺牲代码质量的前提下提高程序性能。实验数据表明,这些优化方法能在内存使用和计算效率方面带来显著改进,适用于大规模数据处理、递归计算等场景。
73 5
Python高性能编程:五种核心优化技术的原理与Python代码
|
4月前
|
存储 索引 Python
Python编程数据结构的深入理解
深入理解 Python 中的数据结构是提高编程能力的重要途径。通过合理选择和使用数据结构,可以提高程序的效率和质量
176 59
|
3月前
|
Python
[oeasy]python055_python编程_容易出现的问题_函数名的重新赋值_print_int
本文介绍了Python编程中容易出现的问题,特别是函数名、类名和模块名的重新赋值。通过具体示例展示了将内建函数(如`print`、`int`、`max`)或模块名(如`os`)重新赋值为其他类型后,会导致原有功能失效。例如,将`print`赋值为整数后,无法再用其输出内容;将`int`赋值为整数后,无法再进行类型转换。重新赋值后,这些名称失去了原有的功能,可能导致程序错误。总结指出,已有的函数名、类名和模块名不适合覆盖赋新值,否则会失去原有功能。如果需要使用类似的变量名,建议采用其他命名方式以避免冲突。
56 14
|
3月前
|
分布式计算 大数据 数据处理
技术评测:MaxCompute MaxFrame——阿里云自研分布式计算框架的Python编程接口
随着大数据和人工智能技术的发展,数据处理的需求日益增长。阿里云推出的MaxCompute MaxFrame(简称“MaxFrame”)是一个专为Python开发者设计的分布式计算框架,它不仅支持Python编程接口,还能直接利用MaxCompute的云原生大数据计算资源和服务。本文将通过一系列最佳实践测评,探讨MaxFrame在分布式Pandas处理以及大语言模型数据处理场景中的表现,并分析其在实际工作中的应用潜力。
129 2

热门文章

最新文章