@songying
2018-07-17T21:31:13.000000Z
字数 2143
阅读 1100
python数据类型
collections.deque类(双向队列)是一个线程安全、可以快速从两端添加或者删除元素的数据类型。
class collections.deque([iterable[, maxlen]])
Add x to the right side of the deque.
append(x)
Add x to the left side of the deque.
appendleft(x)
Remove and return an element from the right side of the deque. If no elements are present, raises an IndexError.
Remove and return an element from the left side of the deque. If no elements are present, raises an IndexError.
Extend the right side of the deque by appending elements from the iterable argument.
extend(iterable)
Extend the left side of the deque by appending elements from iterable. Note, the series of left appends results in reversing the order of elements in the iterable argument.
extendleft(iterable)
Insert x into the deque at position i.
If the insertion would cause a bounded deque to grow beyond maxlen, an IndexError is raised.
Remove all elements from the deque leaving it with length 0.
Create a shallow copy of the deque.
Count the number of deque elements equal to x.
count(x)
Return the position of x in the deque (at or after index start and before index stop). Returns the first match or raises ValueError if not found.
Remove the first occurrence of value. If not found, raises a ValueError.
remove(value)
Reverse the elements of the deque in-place and then return None.
Rotate the deque n steps to the right. If n is negative, rotate to the left.
- n > 0 , 队列的最右边的 n 个元素会被移动到队列的左边
- n < 0, 最左边的 n 个元素会被移动到右边
When the deque is not empty, rotating one step to the right is equivalent to d.appendleft(d.pop()), and rotating one step to the left is equivalent to d.append(d.popleft()).
Maximum size of a deque or None if unbounded.
方法 | 说明 |
---|---|
s.append(e) |
添加一个元素到最右侧(到最后一个元素之后) |
s.appendleft(e) |
添加一个元素到最左侧(到第一个元素之前) |
s.clear() | 删除所有元素 |
s.rotate(n) |
把 n 个元素从队列的一端移到另一端 |
s.count(e) |
s中e出现的次数 |
s.extend(i) |
将可迭代对象 i 中的元素添加到尾部 |
s.extendleft(i) |
将可迭代对象 i 中的元素添加到头部 |
s.pop() |
移除最后一个元素并返回它的值 |
s.popleft() |
移除第一个元素并返回它的值 |
s.remove(e) |
移除序列里第一次出现的 e 元素 |
s.reverse() |
调转序列中元素的位置 |
方法 | 说明 |
---|---|
s.__iadd__(s2) |
s += s2 ,就地拼接 |
s.__setitem__(p, e) |
s[p] = e, 把位于p位置的元素替换成e |
s.__copy__() |
对 copy.copy (浅复制)的支持 |
s.__delitem__(p) |
把位置 p 的元素移除 |
s.__getitem__(p) |
s[p] ,读取位置 p的元素 |
s.__iter__() |
返回迭代器 |
s.__len__() |
len(s) ,序列的长度 |
s.__reversed__() |
返回一个从尾部开始扫描元素的迭代器 |