Fork me on GitHub

Head first python前六章小结

  看这本Head first python已经有十几天了,到第七章开始讲Web开发、移动应用开发,后半年我主要是想往后端的方向发展,所以这本书暂时告一段落。这篇博客没有太多的注释,主要是内容比较简单,只是形式我看着可以借鉴,anyway!!

1.有四个文件,内容是姓名、出生日期、跑步成绩,要处理这些数据,得到这种数据--姓名+成绩前三:

sarah.txt
Sarah Sweeney,2002-6-17,2:58,2.58,2:39,2-25,2-55,2;55,2:54,2.18,2:55,2:55
james.txt
Janmes Hehe,2001-5-12,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22
julie.txt
Julie Xixi,2003-07-29,2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21
mikey.txt
Mikey Tree,2004-01-22,2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38

 2.各函数作用:

经过get_coach_data()处理后的数据:

['Sarah Sweeney', '2002-6-17', '2:58', '2.58', '2:39', '2-25', '2-55', '2;55', '2:54', '2.18']

sanitize()函数将格式规范,set()将列表变为一个集合并去重:
{'2.15', '2.25', '2.18', '2.58', '2.54', '2.39', '2.55'}

3.我觉得这里的返回很有意思,万物都可返回。

返回一个列表:return data.strip().split(',')
返回一个字典:return {'Name': templ.pop(0),'DOB': templ.pop(0)}
返回一个对象:return Athlete(templ.pop(0),templ.pop(0),templ)

 4.返回一个字典时的代码:

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

 5.自定义一个类:

class Athlete:
    def __init__(self, a_name, a_dob=None, a_time=[]):
        self.name = a_name
        self.dob = a_dob
        self.times = a_time

    def top3(self):
        return sorted(set([sanitize(t) for t in self.times]))[0:3]

    def add_time(self, time_value):
        self.times.append(time_value)

    def add_times(self, list_of_times):
        self.times.extend(list_of_times)

def get_coach_data(filename):
    try:
        with open(filename) as f:
            data = f.readline()
        templ = data.strip().split(',')
        return Athlete(templ.pop(0),templ.pop(0),templ)
    except IOError as ioerr:
        print('File error: ' + str(ioerr))
        return None

sarah = get_coach_data('sarah.txt')
# 将sarah.top3()转换为字符串是因为,列表不能加减
print(sarah.name + "'s fastest times are: " + str(sarah.top3()))
vera = Athlete('Vera vi')
vera.add_time('1.31')
print(vera.top3())
vera.add_times(['2.22', "1-21"])
print(vera.top3())

 6.继承list类:

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

class AthleteList(list):
    def __init__(self,a_name,a_dob=None,a_times=[]):
        list.__init__([])
        self.name = a_name
        self.dob = a_dob
        self.extend(a_times)

    def top3(self):
        return sorted(set([sanitize(t) for t in self]))[0:3]

vera = AthleteList('Vera vi')
vera.append('1.31')
print(vera.top3())
vera.extend(['1.33', '2.66', '3.44','5.22'])
print(vera, vera.top3())

 

posted @ 2018-09-30 15:52  法外狂徒  阅读(283)  评论(0编辑  收藏  举报