Q:将下列格式的txt文件,打印出该选手的3个最快跑步时间

james2.txt =>“James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22,2-01,2.01,2:16"
julie2.txt =>Julie Jones,2002-8-17,2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21,3.01,3.02,2:59
mikey2.txt =>Mikey McManus,2002-2-24,2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38,2:40,2.22,2-31
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,2:22,2-21,2.22

注.pop()方法从指定列表位置删除并返回一个数据项。

1.通过数据抽取到列表来实现

def senitize(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()
return(data.strip().split(','))
except IOError as ioerr:
print('File error' +str (ioerr))
return (None) sarah=get_coach_data('sarah2.txt')
(sarah_name,sarah_dob)=sarah.pop(0), sarah.pop(0)
print(sarah_name+"'s fastest times are:"+ str(sorted(set([senitize(t) for t in sarah]))[0:3])) ========== RESTART: C:/Users/eric/Documents/Python/kelly/kelly2.py ==========
Sarah Sweeney's fastest times are:['2.18', '2.21', '2.22']

2. 通过创建字典来实现

def senitize(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()
return(data.strip().split(','))
except IOError as ioerr:
print('File error' +str (ioerr))
return (None) sarah=get_coach_data('sarah2.txt')
sarah_data={}
sarah_data['Name']=sarah.pop(0)
sarah_data['DOB']=sarah.pop(0)
sarah_data['Times']=sarah
print(sarah_data['Name']+"'s fastest times are:"+ str(sorted(set([senitize(t) for t in sarah_data['Times']]))[0:3]))

3.将字典和数据打印全部写入函数

def senitize(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()
user=data.strip().split(',')
user_data={}
user_data['Name']=user.pop(0)
user_data['DOB']=user.pop(0)
user_data['Times']=user
print(user_data['Name']+"'s fastest times are:"+ str(sorted(set([senitize(t) for t in user_data['Times']]))[0:3]))
except IOError as ioerr:
print('File error' +str (ioerr))
return (None) get_coach_data('sarah2.txt')
get_coach_data('james2.txt')
get_coach_data('mikey2.txt')
get_coach_data('julie2.txt') ========== RESTART: C:/Users/eric/Documents/Python/kelly/kelly2.py ==========
Sarah Sweeney's fastest times are:['2.18', '2.21', '2.22']
James Lee's fastest times are:['2.01', '2.16', '2.22']
Mikey McManus's fastest times are:['2.22', '2.31', '2.38']
Julie Jones's fastest times are:['2.11', '2.23', '2.59']

Python 字典和列表的对比应用的更多相关文章

  1. python 字典和列表嵌套用法

    python中字典和列表的使用,在数据处理中应该是最常用的,这两个熟练后基本可以应付大部分场景了.不过网上的基础教程只告诉你列表.字典是什么,如何使用,很少做组合说明. 刚好工作中采集promethe ...

  2. python 字典,列表,集合,字符串,基础进阶

    python列表基础 首先当然是要说基础啦 列表list 1.L.append(object) -> None 在列表末尾添加单个元素,任何类型都可以,包括列表或元组等 2.L.extend(i ...

  3. python 字典、列表、字符串 之间的转换

    1.列表与字符串转换 1)列表转字符串: 将列表中的内容拼接成一个字符串 将列表中的值转成字符串 2)字符串转列表: 用eval转换 将字符串每个字符转成列表中的值 将字符串按分割成列表 2.列表与字 ...

  4. python字典和列表的高级应用

    1.将序列分解为单独的变量 1.1问题 包含n个元素的元组或列表.字符串.文件.迭代器.生成器,将它分解为n个变量 1.2方案 直接通过赋值操作 要求:变量个数要等于元素个数 当执行分解操作时,有时需 ...

  5. python字典和列表使用

    一.字典中健值为列表或字典 1 a.setdefault(key,[]).append(b)--键值是列表 2 a.setdefault(key,{}).append(b)--键值是字典 二.键值为列 ...

  6. python字典推导&&列表推导&&输出随机数

    字典推导: x = ['A', 'B', 'C', 'D'] y = ['Alice', 'Bob', 'Cecil', 'David'] print({i:j for i,j in zip(x,y) ...

  7. python字典和列表使用的要点

    dicts = {} lists = [] dicts['name'] = 'zhangsan' lists.append(dicts) 这时候lists的内容应该是[{'name': 'zhangs ...

  8. python字典中列表追加数据

    dict = {} for i in range(1, 6): if i not in dict: dict[i] = [] for j in range(101, 106): dict[i].app ...

  9. python字典里面列表排序

    #coding=utf8 #获取到的数据库ip,和负载数据,需要按照负载情况排序 a={u'1.8.1.14': [379, 368, 361, 358, 1363], u'9.2.4.3': [42 ...

随机推荐

  1. 跟开涛老师学shiro -- 身份验证

    身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...

  2. leetcode 121. Best Time to Buy and Sell Stock ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  3. 作业:用HTML制作简历

    代码为: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  4. IOS中使用手机号注册

    #import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface KCVVerify : NSObject ...

  5. 黑马程序员——JAVA基础之JDK1.5新特性高级for循环和可变参数

    ------- android培训.java培训.期待与您交流! ---------- 高级for循环   格式: for(数据类型 变量名 : 被遍历的集合(Collection)或者数组) {   ...

  6. 黑马程序员——JAVA基础之数组

    ------- android培训.java培训.期待与您交流! ---------- 数组: 数组的定义: 数组是相同类型数据的集合, 描述的是相同类型的若干个数据按照一定的先后顺序排列组合而成,其 ...

  7. centos 6.4/redhat 6.4 安装gitlab

    一,把所有包升级到最新版本 yum -y upgrade 二,安装最新版ruby 2.1.5 步骤http://my.oschina.net/duolus/blog/348353 三,安装官方给出的o ...

  8. 异构平台同步(Mysql到Oracle)

    Oracle GoldenGate学习之--异构平台同步(MySQL到Oracle) 如图所示:源端采用Mysql库,目标端采用Oracle库 一.OGG安装配置(源端) 1.OGG下载 https: ...

  9. lvm使用总结-转

    由于安装的kvm需要扩容,研究了下lvm的使用. LVM(Logical Volume Manager)逻辑卷管理器,做法是将物理分区通过软件组合未一个独立的大磁盘(VG,卷组),然后把这个大磁盘分成 ...

  10. linux下恢复误删除的文件方法(ext2及ext3)

     linux下恢复误删除的文件方法(ext2及ext3) 2009-12-19 15:23:47 分类: LINUX 如果是ext2文件系统的,直接用debugfs是可以恢复出来的,但对于ext3,d ...