python队列、缺省字典、排序字典

2022-03-11 16:39:59 浏览数 (1)

python队列、缺省字典、排序字典

代码语言:javascript复制
import heapq

class PriorityQueue:
    def __init__(self):
        self._queue = []
        self._index = 0
    def push(self, item, priority):
        heapq.heappush(self._queue, (-priority, self._index, item))
        self._index  = 1
    def pop(self):
        return heapq.heappop(self._queue)[-1]
    def __repr__(self):
        return '_queue={0},_index={1}'.format(self._queue,self._index)

class Item:
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return 'Item({!r})'.format(self.name)

if __name__=='__main__':
    # 优先队列,heapq库
    # heappush(heap, x)       将x压入堆中
    # heappop(heap)   从堆中弹出最小的元素
    # heapify(heap)   让列表具备堆特征
    # heapreplace(heap, x)    弹出最小的元素,并将x压入堆中
    # nlargest(n, iter)       返回iter中n个最大的元素
    # nsmallest(n, iter)      返回iter中n个最小的元素
    nums = [8, 1, 2, 3, 7, 5, 6, 4, 0, 9]
    # 让列表具备堆特征,否则输出不按照堆的特性
    heapq.heapify(nums)
    print(heapq.nlargest(3, nums))
    # [0, 1, 2]
    print(heapq.nsmallest(3, nums))
    # [9, 8, 7]
    heapq.heappush(nums,11)
    # [0, 1, 2, 3, 7, 5, 6, 4, 8, 9, 11]
    heapq.heappop(nums)
    # [1, 3, 2, 4, 7, 5, 6, 11, 8, 9]
    heapq.heapreplace(nums, 12)
    # [2, 3, 5, 4, 7, 12, 6, 11, 8, 9]

    q = PriorityQueue()
    q.push(Item('aaa'), 1)
    # _queue=[(-1, 0, Item('aaa'))],_index=1
    q.push(Item('bbb'), 5)
    # _queue=[(-5, 1, Item('bbb')), (-1, 0, Item('aaa'))],_index=2
    q.push(Item('ccc'), 4)
    # _queue=[(-5, 1, Item('bbb')), (-1, 0, Item('aaa')), (-4, 2, Item('ccc'))],_index=3
    q.push(Item('ddd'), 1)
    # _queue=[(-5, 1, Item('bbb')), (-1, 0, Item('aaa')), (-4, 2, Item('ccc')), (-1, 3, Item('ddd'))],_index=4
    print(q.pop()) # Item('bbb')
    # _queue=[(-4, 2, Item('ccc')), (-1, 0, Item('aaa')), (-1, 3, Item('ddd'))],_index=4
    print(q.pop()) # Item('ccc')
    # _queue=[(-1, 0, Item('aaa')), (-1, 3, Item('ddd'))],_index=4
    print(q.pop()) # Item('aaa')
    #  _queue=[(-1, 3, Item('ddd'))],_index=4
    print(q.pop()) # Item('ddd')
    # _queue=[],_index=4

    from collections import defaultdict
    # defaultdict是Python内建dict类的一个子类,第一个参数为default_factory属性提供初始值,默认为None。
    # 它覆盖一个方法并添加一个可写实例变量。
    # 它的其他功能与dict相同,但会为一个不存在的键提供默认值,从而避免KeyError异常
    bags = ['apple', 'orange', 'cherry', 'apple', 'apple', 'cherry', 'blueberry']
    itemcount = defaultdict(int)
    for fruit in bags:
        itemcount[fruit]  = 1
    # defaultdict(<class 'int'>, {'apple': 3, 'orange': 1, 'cherry': 2, 'blueberry': 1})

    import collections

    d1 = {}
    d1['a'] = 'A'
    d1['c'] = 'C'
    d1['b'] = 'B'
    # d1={'a': 'A', 'c': 'C', 'b': 'B'}
    d2 = {}
    d2['a'] = 'A'
    d2['b'] = 'B'
    d2['c'] = 'C'
    # d2={'a': 'A', 'b': 'B', 'c': 'C'}
    print(d1==d2) # True
    d1 = collections.OrderedDict()
    d1['a'] = 'A'
    d1['c'] = 'C'
    d1['b'] = 'B'
    # OrderedDict([('a', 'A'), ('c', 'C'), ('b', 'B')])
    d2 = collections.OrderedDict()
    d2['a'] = 'A'
    d2['b'] = 'B'
    d2['c'] = 'C'
    # OrderedDict([('a', 'A'), ('b', 'B'), ('c', 'C')])
    print(d1==d2)   #False

0 人点赞