码迷,mamicode.com
首页 > 编程语言 > 详细

python 队列

时间:2016-06-04 12:10:29      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

栈:后进先出

单向队列:先进先出
双向队列:
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 队列

标签:

原文地址:http://www.cnblogs.com/ernest-zhang/p/5558470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!