zoukankan      html  css  js  c++  java
  • python 队列

    栈:后进先出

    单向队列:先进先出
    双向队列:
    import collections
    d = collections.deque()
    d.append('1')
    print(d)
    class:
        append: Add an element to the right side of the deque.
      appendleft: Add an element to the left side of the deque. 
      clear: Remove all elements from the deque. 
      
    def count(self, value): # real signature unknown; restored from __doc__
    """ D.count(value) -> integer -- return number of occurrences of value """
    return 0

    def extend(self, *args, **kwargs): # real signature unknown
    """ Extend the right side of the deque with elements from the iterable """
    pass

    def extendleft(self, *args, **kwargs): # real signature unknown
    """ Extend the left side of the deque with elements from the iterable """ #先进后出
        eg:
           >>>d.extendleft(['1', 'dd', 'cc'])
           deque(['cc', 'dd', '1', '3', '1', '2', 'll', 'aa', 'ss'])
    pass

    def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
    """
    D.index(value, [start, [stop]]) -> integer -- return first index of value.
    Raises ValueError if the value is not present. 返回队列元素的索引值,从0开始。
    """
    return 0

    def insert(self, index, p_object): # real signature unknown; restored from __doc__
    """ D.insert(index, object) -- insert object before index """
    pass

    def pop(self, *args, **kwargs): # real signature unknown
    """ Remove and return the rightmost element. """
    pass

    def popleft(self, *args, **kwargs): # real signature unknown
    """ Remove and return the leftmost element. """
    pass

    def remove(self, value): # real signature unknown; restored from __doc__
    """ D.remove(value) -- remove first occurrence of value. """ 可以是元素可以是索引,从右至左删除遇到的第一个元素,先进先出
    pass

    def reverse(self): # real signature unknown; restored from __doc__
    """ D.reverse() -- reverse *IN PLACE* """ 翻转
    pass

    def rotate(self, *args, **kwargs): # real signature unknown
    """ Rotate the deque n steps to the right (default n=1). If n is negative, rotates left. """将队列右端的n个值移动到左端
    pass
     
    单向队列
    def qsize(self):
            """Return the approximate size of the queue (not reliable!)."""
            self.mutex.acquire()
            n = self._qsize()
            self.mutex.release()
            return n
    
        def empty(self):
            """Return True if the queue is empty, False otherwise (not reliable!)."""
            self.mutex.acquire()
            n = not self._qsize()
            self.mutex.release()
            return n
    
        def full(self):
            """Return True if the queue is full, False otherwise (not reliable!)."""
            self.mutex.acquire()
            n = 0 < self.maxsize == self._qsize()
            self.mutex.release()
            return n
         def put(self, item, block=True, timeout=None):
              """Put an item into the queue.
         def get(self, block=True, timeout=None):
            """Remove and return an item from the queue.
     
    import queue
    d = queue.Queue()
    d.put('123')
    d.put('456')
    print(d.qsize())
    p = d.get()
    print(d) # Queue中的数据无法直接输出,这样的结果是打印的内存中的地址,可以用get按先进先出的顺序拿。
    print(p)
  • 相关阅读:
    列举面向对象中的特殊成员以及应用场景
    python中os和sys模块
    谈谈你对闭包的理解?
    Python面试题(练习一)
    logging模块的作用以及应用场景
    Python的垃圾回收机制
    Python的深浅copy
    rust 支持的CPU架构
    Rust 数据类型
    网站用https访问的问题
  • 原文地址:https://www.cnblogs.com/ernest-zhang/p/5558470.html
Copyright © 2011-2022 走看看