forked from foxli180/HeadFirstPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13-p175.py
More file actions
36 lines (29 loc) · 1.33 KB
/
13-p175.py
File metadata and controls
36 lines (29 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def sanitize(time_string): #序列化得到的无规则数据,将其格式化为 mins.secs 的格式
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return(time_string)
(mins,secs)=time_string.split(splitter)
return(mins+'.'+secs)
def load_from_file(filename): #读取文件的一行(这个文件只有一行,多行咋办)
try:
with open (filename) as f:
data = f.readline()
return (data.strip().split(','))
except IOError as err:
print('File Error: '+str(err))
return (None)
#james = load_from_file('james.txt')
#julie = load_from_file('julie.txt')
#mikey = load_from_file('mikey.txt')
sarah = load_from_file('sarah2.txt')
#列表推导: 先将james 里的每个t格式化为统一的时间,再使用set()剔除重复值,再使用sort进行排序
#使用[0:3]获得列表的前三位
#print(sorted(set([sanitize(t) for t in james]))[0:3])
#print(sorted(set([sanitize(t) for t in julie]))[0:3])
#print(sorted(set([sanitize(t) for t in mikey]))[0:3])
#print(sorted(set([sanitize(t) for t in sarah]))[0:3])
(sarah_name,sarah_dob) = (sarah.pop(0),sarah.pop(0))
print(sarah_name+"'s fastest times are: "+str(sorted(set([sanitize(t) for t in sarah]))[0:3]))