资讯专栏INFORMATION COLUMN

head first python(第六章)–学习笔记

piapia / 1737人阅读

摘要:代码改为根据数据结构,第一个数据是名字,第二个是生日,第二个之后是成绩,所以分别将相关数据赋值到字典里面。是否知道何时使用列表而何时使用字典,这正式从好的程序员中区分出优秀程序员的一个标准。特定函数应用特定数据。更加正规的做法是建立类。

样例数据

Sarah Sweeney,2002-6-17,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55,2:22,2-21,2.22
需要将数据整理,实现人名+出生日期+成绩的输出 以往的做法是:
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]))
这次加入了字典的做法

字典将数据值与键关联:

key   -->   value
Name        "sarah sweeney"
DOB         "2002-6-17"
Times       "[2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55,2:22,2-21,2.22]"

创建字典的方式可以是

cleese = {} #大括号!!

也可以是

palin = dict()

关联key和value的话是

cleese["Name"] = "John Cleese"  # 一个key 对应一个字符串

或者

cleese["Name"] = ["John Cleese","John Cleese1","John Cleese2","John Cleese3","John Cleese4"] #一个key对应一个list

或者

cleese = {"Name":"abc","Address":"asdasdasda"}  #注意是用冒号

另外数据值与key关联后,需要访问数据值里面的某个数据项的话,可以是

cleese["Name"][-1] 

类似多维数组使用。

代码改为

#!/usr/bin/python
# -*- coding: utf-8 -*-


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_data={}
sarah_data["Name"] = sarah.pop(0)   #根据数据结构,第一个数据是名字,第二个是生日,第二个之后是成绩,所以分别将相关数据赋值到字典里面。
sarah_data["DOB"] = sarah.pop(0)
sarah_data["Times"] = sarah

print(sarah_data["Name"] + ""s fastest times are: " + str(sorted(set([sanitize(t) for t in sarah_data["Times"]]))[0:3]))
  

字典的方法优势在于合理使用数据结构。是否知道何时使用列表而何时使用字典,这正式从好的程序员中区分出优秀程序员的一个标准。
字典其实也叫“映射”,“散列”,“关联数组”

为了更加方便的处理多个人的成绩的数据,所以将字典数据转移到函数里面去,直接通过函数生成出字典,并返回需要的数据
#!/usr/bin/python
# -*- coding: utf-8 -*-


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("sarah2.txt")
james = get_coach_data("james2.txt")

print(sarah["Name"] + ""s fastest times are: " + sarah["Times"])

这就是将代码和数据打包在一起。特定函数应用特定数据。

更加正规的做法是建立类。

类是面向对象oop编程模型的东西,类的概念在这里不详细描述。

类可以

1.降低复杂性
2.方便维护和扩展

python的类需要有一个self参数,这个参数是用来标识是属于哪个对象实例的

例如:

class Athlete:
    def __init__(self,value=0):
        self.thing = value      #定义这个类的属性thing
    def how_big(self)           #定义一个方法how_big
        return(len(self.thing))

btw:init 是类的python固定实现方法,所以是必须的。

你写的代码                   -->     python执行的代码
d = Athlete("Holy Grail")           Athlete.__init__(d,"Holy Grail")
                                    |         |      |  
                                    类       方法    目标标识符 
                                    |         |     |
d.how_big()                         Athlete.how_big(d)

代码改为:

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 Athlete:
    def __init__(self, a_name, a_dob=None, a_times=[]):
        self.name = a_name      #通过类的属性来定义name,dob和times
        self.dob = a_dob
        self.times = a_times

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

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)

james = get_coach_data("james2.txt")
julie = get_coach_data("julie2.txt")
mikey = get_coach_data("mikey2.txt")
sarah = get_coach_data("sarah2.txt")

print(james.name + ""s fastest times are: " + str(james.top3()))
print(julie.name + ""s fastest times are: " + str(julie.top3()))
print(mikey.name + ""s fastest times are: " + str(mikey.top3()))
print(sarah.name + ""s fastest times are: " + str(sarah.top3()))
  

科普:

1.通过在各个对象的属性中保留原始数据,可以支持类扩展来满足将来的其他需求。如果处理数据并作为对象初始化代码的一部分,说明你已对程序员将如何使用这个类做出了假设,而日后这些假设肯定会对你造成障碍。

在类里面增加一个灵活的增加成绩数据的函数

可以是增加单个成绩,或是增加多个成绩
单个成绩用add_time,传入的是字符串
多个成绩是add_times,传入的是list

以下是单个成绩的样例:

#!/usr/bin/python
# -*- coding: utf-8 -*-

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

        def add_time(self,time_value):      #这里就是了。
                self.times.append(time_value)
        def top3(self):
                return (sorted(set([sanitize(t) for t in self.times]))[0:15])
        def add_times(self,list_of_times):
                self.times.extend(list_of_times)

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 (Athlete(templ.pop(0),templ.pop(0),templ))
        except IOError as ioerr:
                print("File error:" + str(ioerr))
                return(None)

sarah = get_coach_data("sarah2.txt")

sarah.add_time("2.88")      #这里调用
print(sarah.name + ""s fastest times are: " + str(sarah.top3()))    #输出结果会改变
观察到这个类有点像list,所以有重复制造车轮的嫌疑,并且功能单一,所以决定集成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):    #继续list类,所以这里要写list的名字

    def __init__(self, a_name, a_dob=None, a_times=[]):
        list.__init__([])   #这里需要初始化list类
        self.name = a_name
        self.dob = a_dob
        self.extend(a_times)    #因为集成list类了,所以这里可以直接使用list的extend方法

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

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

james = get_coach_data("james2.txt")
julie = get_coach_data("julie2.txt")
mikey = get_coach_data("mikey2.txt")
sarah = get_coach_data("sarah2.txt")

print(james.name + ""s fastest times are: " + str(james.top3()))
print(julie.name + ""s fastest times are: " + str(julie.top3()))
print(mikey.name + ""s fastest times are: " + str(mikey.top3()))
print(sarah.name + ""s fastest times are: " + str(sarah.top3()))

原文引用:http://www.godblessyuan.com/2015/05/03/head_first_python_chapter_6_lea...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/37545.html

相关文章

  • Python基础教程》六章--读书笔记

    摘要:第六章抽象本章会介绍如何将语句组织成函数。关键字参数和默认值目前为止,我们使用的参数都是位置参数,因为它们的位置很重要,事实上比它们的名字更重要。参数前的星号将所有值放置在同一个元祖中。函数内的变量被称为局部变量。 第六章:抽象 本章会介绍如何将语句组织成函数。还会详细介绍参数(parameter)和作用域(scope)的概念,以及递归的概念及其在程序中的用途。 懒惰即美德 斐波那契数...

    AnthonyHan 评论0 收藏0
  • 流畅的python读书笔记-六章-使用一等函数实现设计模式

    摘要:在复杂的情况下,需要具体策略维护内部状态时,可能需要把策略和享元模式结合起来。函数比用户定义的类的实例轻量,而且无需使用享元模式,因为各个策略函数在编译模块时只会创建一次。 一等函数实现设计模式 经典的策略模式定义 定义一系列算法,把它们一一封装起来,并且使它们可以相互替换。本模式使得算法可以独立于使用它的客户而变化。 案例 假如一个网店制定了下述折扣规则。 有 1000 或以上积分...

    cnsworder 评论0 收藏0
  • Head First Python 学习心得(1-6章)

    摘要:在指定位置删除并返回这个数据项,注意这里是有返回项的。移除某一个特定数据项。第二章发布并上传代码到在查阅大量资料发布和上传还有很多附属文件需要编写和上传以确保模块能够正常发布和更新。包含函数串链,特点是中包含函数。 写在前面:吾尝终日而思矣,不如须臾之所学也;吾尝跂而望矣,不如登高之博见也。登高而招,臂非加长也,而见者远;顺风而呼,声非加疾也,而闻者彰。假舆马者,非利足也,而致千里;假...

    pumpkin9 评论0 收藏0
  • Flask Web开发:六章的电子邮件配置

    摘要:弄了好久终于,踩了很多坑,感觉自己好菜,提供我的参考在外面设置,如,注意没有引号和空格邮箱设置账号获取授权码,在外部传递安全如,注意没有引号和空格发送者邮箱接收者邮箱,,注意没有引号参考的一个作者的文章插件系列,还有廖雪峰的教程 弄了好久终于OK,踩了很多坑,感觉自己好菜,提供我的参考 # -*- coding: utf-8 -*- import os from flask impor...

    airborne007 评论0 收藏0
  • 高程读书笔记 六章 面向对象程序设计

    摘要:创建一个新对象将构造函数的作用域赋给新对象因此就指向了这个新对象执行构造函数中的代码为这个新对象添加属性返回新对象。 本章内容 理解对象属性 理解并创建对象 理解继承 ECMA-262把对象定义为:无序属性的集合,其属性可以包含基本值、对象或者函数 理解对象 创建对象 创建自定义对象的最简单方式就是创建一个Object的实例,再为它添加属性和方法。 var person = new...

    468122151 评论0 收藏0

发表评论

0条评论

piapia

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<