【七】定制数据对象

发布时间:2017-7-1 11:02:44编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"【七】定制数据对象 ",主要涉及到【七】定制数据对象 方面的内容,对于【七】定制数据对象 感兴趣的同学可以参考一下。

一:编写程序

现如今有一组新的秒表数据,需要对其数据进行提取

sarah.txt

sara,2002-9-9,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55

 1.将sarah.txt中的名字与成绩打印出来(使用读取文件的方式)

#coding=utf-8#打印出秒表数据的姓名与前三列的成绩def get_file(filename):    try:        with open(filename) as f:            data=f.readline().strip().split(",")            return data    except IOError as e:        raise edef qxsj(time_string):    if "-" in time_string:        splitter="-"    elif ":" in time_string:        splitter=":"    else:        return time_string    (fen,miao)=time_string.strip().split(splitter)    return(fen+"."+miao)open_file=get_file("D:\pydj\sarah.txt")#移除前两列的数据user_name,user_sr=open_file.pop(0),open_file.pop(0)print(open_file)print(user_name,user_sr)print(user_name+"’fastest times are:"+str(sorted(set([qxsj(i) for i in open_file]))[0:3]))

打印结果:

 完善代码思路:

  1. 一次性完成字典的创建
  2. 把字典创建代码一到get_file()函数中,返回一个字典而不是列表
  3. 把确定各个选手的3个最快时间的代码移到get_file()函数中
  4. 调整主代码中的函数调用,调用这个新版本的get_file()函数来支持新的操作模式

mikey.txt

mikey test,2000-2-2,2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38

 2.将sarah.txt中的名字与成绩打印出来(使用字典的方式)

#coding=utf-8def get_file(filename):    try:        with open(filename) as f:            data=f.readline().strip().split(",")            #返回一个字典,字典中将各个数据提取出来            return({                "Name":data.pop(0),                "DOB":data.pop(0),                "Times":str(sorted(set([qxsj(i) for i in data]))[0:3])            })    except IOError as e:        raise edef qxsj(time_string):    if "-" in time_string:        splitter="-"    elif ":" in time_string:        splitter=":"    else:        return time_string    (fen,miao)=time_string.strip().split(splitter)    return(fen+"."+miao)#sarch的记录sarch_file=get_file("D:\pydj\sarah.txt")print(sarch_file["Name"]+"’fastest times are:"+sarch_file["Times"])#mikey的记录mikey_file=get_file("D:\pydj\mikey.txt")print(mikey_file["Name"]+"’fastest times are:"+mikey_file["Times"]) 

打印结果:

C:\Python27\python.exe D:/pydj/ex5.pysara’fastest times are:['2.18', '2.25', '2.39']mikey test’fastest times are:['2.22', '2.38', '2.49']Process finished with exit code 0

 二:使用class定义类

 创建对象实例

a=Athlete()b=Athlete()c=Athlete()d=Athlete()
  •  小括号告诉python要创建一个新的“Athlete”对象,然后复制给一个变量
  • 所有这些变量都是唯一的,类型都是Athlete

self的重要性

每个方法的第一个参数都是self

 

#定义一个a类In [20]: class a:    ...:     def __init__(self,a_name,a_dob=None,a_times=[]):    ...:         self.name=a_name    ...:         self.dob=a_dob    ...:         self.times=a_times    ...:    #实例化该类     In [21]: sarah=a("sarah","200-2-2",["2:55","2.3","9-6"])#查看sarah的类型,为a类In [22]: type(sarah)Out[22]: __main__.aIn [23]: sarahOut[23]: <__main__.a at 0x7f2df2262a20>In [24]: sarah.nameOut[24]: 'sarah'In [25]: sarah.dobOut[25]: '200-2-2'In [26]: sarah.timesOut[26]: ['2:55', '2.3', '9-6']

 3.将sarah.txt中的名字与成绩打印出来(使用类的方式)

#coding=utf-8#编写代码来定义Athlete类#除了__init__()方法外,还要定义一个新方法top3(),调用这个方法会返回最快的3个时间#要调整dakaiwenjian()函数,返回Athlete对象而不是字典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 top3(self):        return str(sorted(set([fenge(i) for i in self.times]))[0:3])def dakaiwenjian(filename):    try:        with open(filename,"r") as f:            data=f.readline().strip().split(",")            return(Athlete(a_name=data.pop(0),a_dob=data.pop(0),a_times=data))    except IOError as e:        raise edef fenge(time_string):    if "-" in time_string:        splitter="-"    elif ":" in time_string:        splitter=":"    else:        return time_string    (fen,miao)=time_string.strip().split(splitter)    return(fen+"."+miao)#sarch的记录sarch_file=dakaiwenjian("D:\pydj\sarah.txt")print(sarch_file.name+"’fastest times are:"+sarch_file.top3())#mikey的记录mikey_file=dakaiwenjian("D:\pydj\mikey.txt")print(mikey_file.name+"’fastest times are:"+mikey_file.top3())

 打印结果:

C:\Python27\python.exe D:/pydj/ex8.pysara’fastest times are:['2.18', '2.25', '2.39']mikey test’fastest times are:['2.22', '2.38', '2.49']Process finished with exit code 0

 4.向你的类增加两个方法,一个为add_time(),将一个额外的计时值追加到选手的计时数据。第二个方法add_times()会用一个或多个计时值(提供一个列表)来扩展一个选手的计时数据

#coding=utf-8class  Athlete:    def __init__(self,a_name,a_dob=None,a_times=[]):        self.name=a_name        self.dob=a_dob        self.times=a_times    #前三名成绩    def top3(self):        return str(sorted(set([fenge(i) for i 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 dakaiwenjian(filename):    try:        with open(filename,"r") as f:            data=f.readline().strip().split(",")            return(Athlete(a_name=data.pop(0),a_dob=data.pop(0),a_times=data))    except IOError as e:        raise edef fenge(time_string):    if "-" in time_string:        splitter="-"    elif ":" in time_string:        splitter=":"    else:        return time_string    (fen,miao)=time_string.strip().split(splitter)    return(fen+"."+miao)#sarch的记录sarch_file=dakaiwenjian("D:\pydj\sarah.txt")#为sarch增加一个秒表时间sarch_file.add_time("1.11")print(sarch_file.name+"’fastest times are:"+sarch_file.top3())#mikey的记录mikey_file=dakaiwenjian("D:\pydj\mikey.txt")print(mikey_file.name+"’fastest times are:"+mikey_file.top3())

 打印结果:

C:\Python27\python.exe D:/pydj/ex8.py
sara’fastest times are:['1.11', '2.18', '2.25']
mikey test’fastest times are:['2.22', '2.38', '2.49']

Process finished with exit code 0

 三:继承类

 5.继承python内置的list

In [28]: class Namelist(list):    ...:     def __init__(self,a_name):    ...:         list.__init__([])    ...:         self.name=a_name    ...:         #实例化类In [29]: huahua=Namelist("qwe")In [30]: type(huahua)Out[30]: __main__.Namelist#该实例的方法In [31]: dir(huahua)Out[31]: ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'name', 'pop', 'remove', 'reverse', 'sort']In [32]: huahuaOut[32]: []#append方法In [33]: huahua.append("one")In [34]: huahuaOut[34]: ['one']#extend方法In [35]: huahua.extend(["two","three"])In [36]: huahuaOut[36]: ['one', 'two', 'three']

 6.把原有的Athlete类的代码删除,写一个新的Atheletelist类,让它继承内置的list类,然后进行测试

#coding=utf-8class  Athletelist(list):    def __init__(self,a_name,a_dob=None,a_times=[]):        list.__init__(self)        self.name=a_name        self.dob=a_dob        #因为该类继承了list,所以不需要将a_times在重新赋值,直接将a_times,extend        self.extend(a_times)    #前三名成绩    def top3(self):        return str(sorted(set([fenge(i) for i in self]))[0:3])def dakaiwenjian(filename):    try:        with open(filename,"r") as f:            data=f.readline().strip().split(",")            return(Athletelist(a_name=data.pop(0),a_dob=data.pop(0),a_times=data))    except IOError as e:        raise edef fenge(time_string):    if "-" in time_string:        splitter="-"    elif ":" in time_string:        splitter=":"    else:        return time_string    (fen,miao)=time_string.strip().split(splitter)    return(fen+"."+miao)#sarch的记录sarch_file=dakaiwenjian("D:\pydj\sarah.txt")#为sarch增加一个秒表时间print(sarch_file.name+"’fastest times are:"+sarch_file.top3())#mikey的记录mikey_file=dakaiwenjian("D:\pydj\mikey.txt")print(mikey_file.name+"’fastest times are:"+mikey_file.top3())

打印结果:

C:\Python27\python.exe D:/pydj/ex8.pysara’fastest times are:['2.18', '2.25', '2.39']mikey test’fastest times are:['2.22', '2.38', '2.49']Process finished with exit code 0


上一篇:【转】PHP中file_put_contents追加和换行
下一篇:LeetCode——Construct Binary Tree from Preorder and Inorder Traversal

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款