Head First Python:第5章:推导数据处理数据

"""时间列表进行格式化,统一格式为mins.secs"""

def sanitize(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)

"""读取文件,并将记录时间转换成列表,以下这部分代码可以由函数get_coach_data()替换为如:sarah=get_coach_data('sarah.txt')"""

"""with open('james.txt') as jaf:

   data=jaf.readline()

   james=data.strip().split(',')

with open('julie.txt') as juf:

   data=juf.readline()

   julie=data.strip().split(',')

with open('mikey.txt') as mif:

   data=mif.readline()

   mikey=data.strip().split(',')

with open('sarah.txt') as saf:

   data=saf.readline()

   sarah=data.strip().split(',')"""

james=get_coach_data('james.txt')

julie=get_coach_data('julie.txt')

mikey=get_coach_data('mikey.txt')

sarah=get_coach_data('sarah.txt')

"""

print(james)

print(julie)

print(mikey)

print(sarah)

"""

"""---------臃肿的部分begin------------"""

"""

clean_james=[]

clean_julie=[]

clean_mikey=[]

clean_sarah=[]

for each_t in james:

   clean_james.append(sanitize(each_t))

for each_t in julie:

   clean_julie.append(sanitize(each_t))

for each_t in mikey:

   clean_mikey.append(sanitize(each_t))

for each_t in sarah:

   clean_sarah.append(sanitize(each_t))

"""

"""---------臃肿的部分end------------"""

"""------利用列表推到优化后去除冗余代码,列表推导从右至左读代码begin----"""

"""

print(sorted([sanitize(each_t) for each_t in james]))

print(sorted([sanitize(each_t) for each_t in julie]))

print(sorted([sanitize(each_t) for each_t in mikey]))

print(sorted([sanitize(each_t) for each_t in sarah]))

"""

"""------利用列表推到优化后去除冗余代码,列表推导从右至左读代码end----"""

"""-------去除重复项,输出前三项最大值,但是重复冗余代码太多begin-------"""

"""

james=sorted([sanitize(each_t) for each_t in james])

julie=sorted([sanitize(each_t) for each_t in julie]

你可能感兴趣的:(Head First Python:第5章:推导数据处理数据)