勿喷格式,参考第四章
import os
os.chdir('../' )
# data = [6, 3, 1, 2, 4, 5]
#原地排序
#data.sort()
#复制排序
# data2 = sorted(data)
# print(data)
# print(data2)
#清洗数据
def sanitize(time_string):
if '_' in time_string:
splitter = '_'
elif ':' in time_string:
splitter = ':'
else :
return (time_string);
(min, secs) = time_string.split(splitter)
return (min + '.' + secs)
#从文件读取数据并打印
def print_time (file_name):
try :
with open (file_name) as name:
data = name.readline()
# print(data.strip().split(','))
return data.strip().split(',' )
except IOError as ioerr:
print ('File Err:' + str (ioerr))
return (None )
# print_time('james.txt')
#数据排序
def cleanData (arr):
clean_data = []
'''for each_t in arr:
print(sanitize(each_t))
clean_data.append(sanitize(each_t))'''
# 列表推倒
clean_data = [sanitize(each_t) for each_t in arr]
return sorted (clean_data)
# print(sorted(clean_data))
#应用列表推倒获取去重后的前三项
unique_james = []
# for each_t in cleanData(print_time('james.txt')):
# if each_t not in unique_james:z
# unique_james.append(each_t)
# print(unique_james[0:3])
#应用set()工厂函数获取去重后的前三项
unique_james2 = set (cleanData(print_time('james.txt' )))
print (sorted (unique_james2)[0 :3 ]) #set()为无序所以重新排序
# print(cleanData(print_time('james.txt')))
另附一个支持文件: