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)
  • 相关阅读:
    G++与VS2015在变量作用域上的差异性
    SO_REUSEADDR与SO_REUSEPORT平台差异性与测试
    带着SMART原则重新出发
    动态语言的灵活性是把双刃剑 -- 以Python语言为例
    程序员必知的Python陷阱与缺陷列表
    MySQL添加字段和修改字段
    java poi给sheet表格中的某个单元格添加批注
    Maven入门:使用Nexus搭建Maven私服及上传下载jar包
    Linux上两种网络连接方式
    linux创建账户并自动生成主目录和主目录下的文件
  • 原文地址:https://www.cnblogs.com/ernest-zhang/p/5558470.html
Copyright © 2011-2022 走看看