python数字类型图解_Python基本数据类型(四)

5、双向队列(deque)函数说明

一个线程安全的双向队列,可进可出,可以从两端添加和删除元素;class deque(object):

"""

deque([iterable[, maxlen]]) --> deque object

Build an ordered collection with optimized access from its endpoints.

提供了两端都可以操作的序列,这意味着,可以在序列前后都执行添加或删除;

"""

def append(self, *args, **kwargs): # real signature unknown

""" Add an element to the right side of the deque. """

pass

‘‘‘

在deque的右边添加一个元素;

例如:

>>> from collections import deque

>>> d = deque()

>>> d

deque([])

>>> d.append(‘a‘)

>>> d

deque([‘a‘])

>>> d.append(‘b‘)

>>> d

deque([‘a‘, ‘b‘])

>>> d.append([‘b‘,‘c‘])

>>> d

deque([‘a‘, ‘a‘, [‘b‘, ‘c‘]])

>>> d.append(‘b‘,‘c‘)

Traceback (most recent call last):

File "", line 1, in 

TypeError: append() takes exactly one argument (2 given)

‘‘‘

def appendleft(self, *args, **kwargs): # real signature unknown

""" Add an element to the left side of the deque. """

pass

‘‘‘

在deque的左边添加一个元素;

例如:

>>> d = deque(‘a‘)

>>> d

deque([‘a‘])

>>> d.appendleft(‘b‘)

>>> d

deque([‘b‘, ‘a‘])

‘‘‘

def clear(self, *args, **kwargs): # real signature unknown

""" Remove all elements from the deque. """

pass

‘‘‘

从deque中删除所有元素;

例如:

>>> d = deque([‘a‘,‘b‘])

>>> d

deque([‘a‘, ‘b‘])

>>> d.clear()

>>> d

deque([])

‘‘‘

def count(self, value): # real signature unknown; restored from __doc__

""" D.count(value) -> integer -- return number of occurrences of value """

return 0

‘‘‘

返回值的出现次数;

例如:

>>> d = deque([‘a‘,‘a‘])

>>> d.count(‘a‘)

2

‘‘‘

def extend(self, *args, **kwargs): # real signature unknown

""" Extend the right side of the deque with elements from the iterable """

pass

‘‘‘

使用可迭代的元素扩展deque的右侧,即一次性可添加多个元素;

例如:

>>> d = deque([‘a‘,‘b‘])

>>> d

deque([‘a‘, ‘b‘])

>>> d.extend([‘c‘,‘d‘,‘e‘])

>>> d

deque([‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘])

‘‘‘

def extendleft(self, *args, **kwargs): # real signature unknown

""" Extend the left side of the deque with elements from the iterable """

pass

‘‘‘

使用可迭代的元素扩展deque的左侧,即一次性可添加多个元素;

例如:

>>> d = deque([‘a‘,‘b‘])

>>> d

deque([‘a‘, ‘b‘])

>>> d.extendleft([‘c‘,‘d‘,‘e‘])

>>> d

deque([‘e‘, ‘d‘, ‘c‘, ‘a‘, ‘b‘])

‘‘‘

def pop(self, *args, **kwargs): # real signature unknown

""" Remove and return the rightmost element. """

pass

‘‘‘

删除并返回最右侧的元素;

例如:

>>> d = deque([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘])

>>> d.pop()

‘e‘

‘‘‘

def popleft(self, *args, **kwargs): # real signature unknown

""" Remove and return the leftmost element. """

pass

‘‘‘

删除并返回最左侧的元素;

例如:

>>> d = deque([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘])

>>> d.popleft()

‘a‘

‘‘‘

def remove(self, value): # real signature unknown; restored from __doc__

""" D.remove(value) -- remove first occurrence of value. """

pass

‘‘‘

删除指定的一个值;

例如:

>>> d = deque([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘])

>>> d.remove()

Traceback (most recent call last):

File "", line 1, in 

TypeError: remove() takes exactly one argument (0 given)

>>> d.remove(‘a‘)

>>> d

deque([‘b‘, ‘c‘, ‘d‘, ‘e‘])

>>> d.remove(‘c‘)

>>> d

deque([‘b‘, ‘d‘, ‘e‘])

>>> d.remove(‘b‘,‘d‘)

Traceback (most recent call last):

File "", line 1, in 

TypeError: remove() takes exactly one argument (2 given)

‘‘‘

def reverse(self): # real signature unknown; restored from __doc__

""" D.reverse() -- reverse *IN PLACE* """

pass

‘‘‘

将元素反转;

例如:

>>> d = deque([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘])

>>> d.reverse()

>>> d

deque([‘e‘, ‘d‘, ‘c‘, ‘b‘, ‘a‘])

‘‘‘

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. """

pass

‘‘‘

将最后的n个元素向右反转(默认n = 1),如果n为负,则将最前的n个元素向左反转;

例如:

>>> d = deque([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘])

>>> d.rotate()

>>> d

deque([‘e‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘])

>>> d.rotate(2)

>>> d

deque([‘c‘, ‘d‘, ‘e‘, ‘a‘, ‘b‘])

>>> d.rotate(-4)

>>> d

deque([‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘a‘])

‘‘‘

def __copy__(self, *args, **kwargs): # real signature unknown

""" Return a shallow copy of a deque. """

pass

‘‘‘

浅拷贝;

‘‘‘

def __delitem__(self, y): # real signature unknown; restored from __doc__

""" x.__delitem__(y) <==> del x[y] """

pass

‘‘‘

删除元素,等同于del;

例如:

>>> d = deque([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘])

>>> d.__delitem__(1)

>>> d

deque([‘a‘, ‘c‘, ‘d‘, ‘e‘])

>>> del d[2]

>>> d

deque([‘a‘, ‘c‘, ‘e‘])

‘‘‘

def __eq__(self, y): # real signature unknown; restored from __doc__

""" x.__eq__(y) <==> x==y """

pass

‘‘‘

等同于判断,返回布尔值;

例如:

>>> x = deque([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘])

>>> y = deque([‘a‘,‘b‘,‘c‘,‘d‘])

>>> x.__eq__(y)

False

>>> x == y

False

>>> y = deque([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘])

>>> x == y

True

‘‘‘

def __getattribute__(self, name): # real signature unknown; restored from __doc__

""" x.__getattribute__(‘name‘) <==> x.name """

pass

def __getitem__(self, y): # real signature unknown; restored from __doc__

""" x.__getitem__(y) <==> x[y] """

pass

‘‘‘

返回队列指定下标的值,下标值需指定并为整数型,否则报错;

例如:

>>> x = deque([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘])

>>> x.__getitem__(2)

‘c‘

>>> x[3]

‘d‘

‘‘‘

def __ge__(self, y): # real signature unknown; restored from __doc__

""" x.__ge__(y) <==> x>=y """

pass

‘‘‘

大于等于判断,返回布尔值;

例如:

>>> x = deque([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘])

>>> y = deque([‘a‘,‘b‘,‘c‘,‘d‘])

>>> x.__ge__(y)

True

>>> x >= y

True

‘‘‘

def __gt__(self, y): # real signature unknown; restored from __doc__

""" x.__gt__(y) <==> x>y """

pass

‘‘‘

大于判断,返回布尔值;

‘‘‘

def __iadd__(self, y): # real signature unknown; restored from __doc__

""" x.__iadd__(y) <==> x+=y """

pass

‘‘‘

自加,相当于+=;

‘‘‘

def __init__(self, iterable=(), maxlen=None): # known case of _collections.deque.__init__

"""

deque([iterable[, maxlen]]) --> deque object

Build an ordered collection with optimized access from its endpoints.

# (copied from class doc)

"""

pass

‘‘‘

构造方法,执行x = deque()时自动调用deque函数;

‘‘‘

def __iter__(self): # real signature unknown; restored from __doc__

""" x.__iter__() <==> iter(x) """

pass

‘‘‘

在deque上返回一个迭代器;

‘‘‘

def __len__(self): # real signature unknown; restored from __doc__

""" x.__len__() <==> len(x) """

pass

‘‘‘

返回队列长度;

例如:

>>> x = deque([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘])

>>> x.__len__()

5

>>> len(x)

5

‘‘‘

def __le__(self, y): # real signature unknown; restored from __doc__

""" x.__le__(y) <==> x<=y """

pass

‘‘‘

小于等于判断,返回布尔值;

‘‘‘

def __lt__(self, y): # real signature unknown; restored from __doc__

""" x.__lt__(y) <==> x

pass

‘‘‘

小于判断,返回布尔值;

‘‘‘

@staticmethod # known case of __new__

def __new__(S, *more): # real signature unknown; restored from __doc__

""" T.__new__(S, ...) -> a new object with type S, a subtype of T """

pass

def __ne__(self, y): # real signature unknown; restored from __doc__

""" x.__ne__(y) <==> x!=y """

pass

‘‘‘

不等于判断,返回布尔值;

‘‘‘

def __reduce__(self, *args, **kwargs): # real signature unknown

""" Return state information for pickling. """

pass

‘‘‘

返回pickling的状态信息;

‘‘‘

def __repr__(self): # real signature unknown; restored from __doc__

""" x.__repr__() <==> repr(x) """

pass

‘‘‘

转化为解释器可读取的形式,即转换为字符串格式;

‘‘‘

def __reversed__(self): # real signature unknown; restored from __doc__

""" D.__reversed__() -- return a reverse iterator over the deque """

pass

‘‘‘

在deque上返回一个反向迭代器;

‘‘‘

def __setitem__(self, i, y): # real signature unknown; restored from __doc__

""" x.__setitem__(i, y) <==> x[i]=y """

pass

‘‘‘

对队列里,指定的下标值进行修改,i需为整数,并且不能超过队列的下标范围;

例如:

>>> x = deque([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘])

>>> x.__setitem__(2,‘f‘)

>>> x

deque([‘a‘, ‘b‘, ‘f‘, ‘d‘, ‘e‘])

>>> x[4] = ‘g‘

>>> x

deque([‘a‘, ‘b‘, ‘f‘, ‘d‘, ‘g‘])

>>> x[5] = ‘h‘

Traceback (most recent call last):

File "", line 1, in 

IndexError: deque index out of range

‘‘‘

def __sizeof__(self): # real signature unknown; restored from __doc__

""" D.__sizeof__() -- size of D in memory, in bytes """

pass

‘‘‘

获取内存中队列的大小,以字节为单位;

‘‘‘

maxlen = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

"""maximum size of a deque or None if unbounded"""

__hash__ = None

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值