码迷,mamicode.com
首页 > 其他好文 > 详细

【七】定制数据对象

时间:2017-06-27 19:59:51      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:hle   image   ring   format   写代码   模式   mat   a20   定义   

一:编写程序

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

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 e
def 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-8
def 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 e
def 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.py
sara’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__.a
In [23]: sarah
Out[23]: <__main__.a at 0x7f2df2262a20>
In [24]: sarah.name
Out[24]: sarah
In [25]: sarah.dob
Out[25]: 200-2-2
In [26]: sarah.times
Out[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 e
def 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.py
sara’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-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 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 e
def 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]: huahua
Out[32]: []
#append方法
In [33]: huahua.append("one")
In [34]: huahua
Out[34]: [one]
#extend方法
In [35]: huahua.extend(["two","three"])
In [36]: huahua
Out[36]: [one, two, three]

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

#coding=utf-8
class  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 e
def 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.py
sara’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

【七】定制数据对象

标签:hle   image   ring   format   写代码   模式   mat   a20   定义   

原文地址:http://www.cnblogs.com/8013-cmf/p/7081270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!