第176页 第六章 定制数据对象
- 章节名:第六章 定制数据对象
- 页码:第176页
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) 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([sanitize(t) for t in sarah]))[0:3]))vs. mine:
#coding=utf-8 def list_score(f): try: with open(f, 'r') as source: scores = [] data = source.readline() init_scores = data.strip().split(',') for score in init_scores: if '-' in score: scores.append(score.replace('-','.')) elif ':' in score: scores.append(score.replace(':','.')) else: scores.append(score) return scores except IOError as err: print str(err) return None james = list_score('james.txt') julie = list_score('julie.txt') mikey = list_score('mikey.txt') sarah = list_score('sarah.txt') uniq_james = [x for x in james if james.count(x) == 1] print sorted(james) print sorted(uniq_james[0:3])
23人阅读
肉肉打太极对本书的所有笔记 · · · · · ·
-
第138页 第四章 持久存储
#文件I/O用pickle# Python术语 - ‘腌制’ 将数据对象保存到一个持久存储中的过程 - ‘解除腌...
-
第172页 第五章 理解数据
#列表和集合# Python术语 - ‘原地’排序,转换然后替换 #sort() - ‘复制'排序,转换然后返...
-
第176页 第六章 定制数据对象
-
第187页 第六章 打包代码和数据
# the following code had been modified to work under Python 2.(7) def sanitize(time_str...
-
第210页 第六章 打包代码和数据
def sanitize(time_string): if '-' in time_string: splitter = '-' elif ':' in time_strin...
> 查看全部8篇
说明 · · · · · ·
表示其中内容是对原文的摘抄