HeadfirstPython---5推导数据6定制数据对象

打开文件读取文件

with open("james.txt") as james:
    jam = james.readline().strip().split(",")
with open("julie.txt") as julie:
    jul = julie.readline().strip().split(",")
with open("mikey.txt") as mikey:
    mik = mikey.readline().strip().split(",")
with open("sarah.txt") as sarah:
    sar = sarah.readline().strip().split(",")

print(jam)
print(jul)
print(mik)
print(sar)

--------------------------------------------------
['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22']
['2.59', '2.11', '2:11', '2:23', '3-10', '2-23', '3:10', '3.21', '3-21']
['2:22', '3.01', '3:01', '3.02', '3:02', '3.02', '3:22', '2.49', '2:38']
['2:58', '2.58', '2:39', '2-25', '2-55', '2:54', '2.18', '2:55', '2:55']

整理数据,排序

def analyzeTime(timeer):
    if '-' in timeer:
        splitter = '-'
    elif ':' in timeer:
        splitter = ':'
    else:
        return timeer
    (min,sec) = timeer.split(splitter)
    return (min + '.' + sec)           #将数据全部变成小数形式

def sortTime(tim):
    tims = []
    for i in tim:
        i = analyzeTime(i)
        tims.append(i)
    tims.sort()                      #用自带函数排序
    print(tims)
    print(tims[0])

sortTime(jam)
sortTime(jul)
sortTime(mik)
sortTime(sar)
*************************************************
['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']
2.01
['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']
2.11
['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22']
2.22
['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']
2.18

有趣的列表推导

可能用到的话会很方便

mins = [1,2,3]
secs = [m*60 for m in mins]
print(secs)

lower = ['I',"don't",'like','spam']
upper = [s.upper() for s in lower]
print(upper)
--------------------------------------------------------
[60, 120, 180]
['I', "DON'T", 'LIKE', 'SPAM']

消除重复数据

def quchong(a):
    b=[]
    for i in a:
        if i not in b:
            b.append(i)
    return b

jam = quchong(jam)
jul = quchong(jul)
mik = quchong(mik)
sar = quchong(sar)

print(jam)
print(jul)
print(mik)
print(sar)

print(jam[0:3])
print(jul[0:3])
print(mik[0:3])
print(sar[0:3])
-------------------------------------------
['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']
2.01
['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']
2.11
['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22']
2.22
['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']
2.18
['2.01', '2.22', '2.34', '2.45', '3.01', '3.10', '3.21']
['2.11', '2.23', '2.59', '3.10', '3.21']
['2.22', '2.38', '2.49', '3.01', '3.02', '3.22']
['2.18', '2.25', '2.39', '2.54', '2.55', '2.58']
['2.01', '2.22', '2.34']
['2.11', '2.23', '2.59']
['2.22', '2.38', '2.49']
['2.18', '2.25', '2.39']

用集合删除重复项

集合是一种数据类型,
特性:集合中的数据是无序的,而且是不允许重复的
如果向集合添加一个数据项,而集合中已经包含这个数据项,Python就会将它忽略

a = set(jam)#也可以

a = set()
for i in jam:
    a.add(i)
print(a)
------------------------
{'2.01', '2.22', '3.10', '3.01', '2.45', '3.21', '2.34'}

sort()方法可以在原地改变列表的顺序
sorted() BIF通过提供复制排序可以对几乎任何数据结构排序
向sort 或者 sorted 传入reverse = TRUE 可以按降序排列数据

打包代码和数据

字典 dict 无序结构,只关注键值对的关联问题

join = ["join",'1997','2.18','2.56','2.34']
a = {}
a['name']=join.pop(0)
a['date']=join.pop(0)
a['time']=join

print(a)
---------------------------------------
{'name': 'join', 'date': '1997', 'time': ['2.18', '2.56', '2.34']}

面向对象的类

类的定义和实例化和其他语言没什么区别
class Athlete():
def init(self):
pass
print(“qq”)
a = Athlete()

让类继承内置类,拥有内置类的属性方法
类继承内置list类

class Abc(list):            #提供类名
    def __init__(self,name):
        list.__init__([])   #初始化派生的类
        self.name = name

a = Abc("hello")
a.append(1)                 #对象同时拥有类的属性和派生内置类的属性方法
a.extend([2,3,4,5,6])

print(a.name)
print(a)
print(dir(a))
------------------------------------------------------
hello
[1, 2, 3, 4, 5, 6]
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__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']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值