[从头学数学] 第244节 Python实现数据结构:列表

剧情提要:
阿伟看到了一本比较有趣的书,是关于《计算几何》的,2008年由北清派出版。很好奇
它里面讲了些什么,就来看看啦。


正剧开始:
星历2016年07月24日 11:03:16, 银河系厄尔斯星球中华帝国江南行省。

[工程师阿伟]正在和[机器小伟]一起研究[计算几何]]。






<span style="font-size:18px;">class PyList:
    def info(self):
        print(self.items);
        
    def __init__(self, contents=[], size=10):
        # The contents allows the programmer to construct a list with
        # the initial contents of this value. The initial_size
        # lets the programmer pick a size for the internal size of the
        # list. This is useful if the programmer knows he/she is going
        # to add a specific number of items right away to the list.
        self.items = [None] * size
        self.numItems = 0
        self.size = size

        for e in contents:
            self.append(e)

    def __getitem__(self,index):
        if index >= 0 and index < self.numItems:
            return self.items[index]

        raise IndexError("PyList index out of range")

    def __setitem__(self,index,val):
        if index >= 0 and index < self.numItems:
            self.items[index] = val
            return

        raise IndexError("PyList assignment index out of range")

    def __add__(self,other):
        result = PyList(size=self.numItems+other.numItems)

        for i in range(self.numItems):
            result.append(self.items[i])

        for i in range(other.numItems):
            result.append(other.items[i])

        return result

    # This method is hidden since it starts with two underscores.
    # It is only available to the class to use.
    def __makeroom(self):
        # increase list size by 1/4 to make more room.
        # add one in case for some reason self.size is 0.
        newlen = (self.size // 4) + self.size + 1
        newlst = [None] * newlen
        for i in range(self.numItems):
            newlst[i] = self.items[i]

        self.items = newlst
        self.size = newlen

    def removeEmpty(self):
        self.size = self.numItems;
        self.items = self.items[:self.numItems];

    def append(self,item):
        if self.numItems == self.size:
            self.__makeroom()

        self.items[self.numItems] = item
        self.numItems += 1
        
    def insert(self,i,e):
        if self.numItems == self.size:
            self.__makeroom()

        if i < self.numItems:
            for j in range(self.numItems-1,i-1,-1):
                self.items[j+1] = self.items[j]

            self.items[i] = e
            self.numItems += 1
        else:
            self.append(e)
            
    def __delitem__(self, index):
        for i in range(index, self.numItems-1):
            self.items[i] = self.items[i+1]
        self.numItems -= 1

    def __eq__(self,other):
        if type(other) != type(self):
            return False

        if self.numItems != other.numItems:
            return False

        for i in range(self.numItems):
            if self.items[i] != other.items[i]:
                return False

        return True

    def __iter__(self):
        for i in range(self.numItems):
            yield self.items[i]

    def __len__(self):
        return self.numItems;

    def __contains__(self,item):
        for i in range(self.numItems):
            if self.items[i] == item:
                return True

        return False

    def __str__(self):
        s = "["
        for i in range(self.numItems):
            s = s + repr(self.items[i])
            
            if i < self.numItems - 1:
                s = s + ", "
                
        s = s + "]"
        return s

    def __repr__(self):
        s = "PyList(["
        for i in range(self.numItems):
            s = s + repr(self.items[i])
            if i < self.numItems - 1:
                s = s + ", "
        s = s + "])"
        return s</span>


<span style="font-size:18px;">def main():
    #计时开始
    startTime = time.clock();

    a = list(range(18));
    pyList = PyList(a);
    pyList.info();

    b = list(range(-10, 10, 3));
    pyList_2 = PyList(b);
    c = pyList+pyList_2;
    c.info();
    c[8] = 5;
    c[2] = 100;
    c.info();
    c.insert(3, -1000);
    c.info();

    for i in range(len(c)):
        print(c[i], end = ' ');
    
    #计时结束
    endTime = time.clock();

    #打印结果
    print('操作用时:{0:.3e} s'.format(endTime-startTime));</span>


本节到此结束,欲知后事如何,请看下回分解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值