yyyyyyyyyyyyyyyyyyyy

博客园 首页 新随笔 联系 订阅 管理

Python sorted list的实现


具体思路是用二分保list有序+插入

class SortedList(list):
    K = -1

    def __init__(self, K=-1):
        list.__init__(self)
        if K != -1: self.K = K

    def append(self, x):
        bisect.insort(self, x)
        if self.K != -1:
            if len(self)==self.K+1:
                self.pop(0)

这里还有一个限size的操作,之前写了一个self=self[1:],结果不对。还不清楚是为什么,留一个问题在这里。

参考

posted on 2017-07-22 13:14  xxxxxxxx1x2xxxxxxx  阅读(551)  评论(0编辑  收藏  举报