您的位置:首页 > 编程语言 > Python开发

自学Python2.7-collections系列

2017-08-22 17:29 288 查看

Python collections系列

Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几个额外的数据类型:
1.Counter: 计数器,主要用来计数
2.OrderedDict: 有序字典
3.defaultdict: 带有默认值的字典
4.namedtuple(): 可命名元组,生成可以使用名字来访问元素内容的tuple子类
5.deque: 双端队列,可以快速的从另外一侧追加和推出对象

一、Counter: 计数器

    Counter是对字典类型的补充,用于追踪值的出现次数。   ps:具备字典的所有功能 + 自己的功能

class deque(object):
"""
deque([iterable[, maxlen]]) --> deque object

A list-like sequence optimized for data accesses near its endpoints.
"""
def append(self, *args, **kwargs): # real signature unknown
""" Add an element to the right side of the deque. """
pass

def appendleft(self, *args, **kwargs): # real signature unknown
""" Add an element to the left side of the deque. """
pass

def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from the deque. """
pass

def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of a deque. """
pass

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

def __add__(self, *args, **kwargs): # real signature unknown
""" Return self+value. """
pass

def __bool__(self, *args, **kwargs): # real signature unknown
""" self != 0 """
pass

def __contains__(self, *args, **kwargs): # real signature unknown
""" Return key in self. """
pass

def __copy__(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of a deque. """
pass

def __delitem__(self, *args, **kwargs): # real signature unknown
""" Delete self[key]. """
pass

def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass

def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass

def __getitem__(self, *args, **kwargs): # real signature unknown
""" Return self[key]. """
pass

def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass

def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass

def __iadd__(self, *args, **kwargs): # real signature unknown
""" Implement self+=value. """
pass

def __imul__(self, *args, **kwargs): # real signature unknown
""" Implement self*=value. """
pass

def __init__(self, iterable=(), maxlen=None): # known case of _collections.deque.__init__
"""
deque([iterable[, maxlen]]) --> deque object

A list-like sequence optimized for data accesses near its endpoints.
# (copied from class doc)
"""
pass

def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass

def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass

def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass

def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass

def __mul__(self, *args, **kwargs): # real signature unknown
""" Return self*value.n """
pass

@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object.  See help(type) for accurate signature. """
pass

def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass

def __reduce__(self, *args, **kwargs): # real signature unknown
""" Return state information for pickling. """
pass

def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass

def __reversed__(self): # real signature unknown; restored from __doc__
""" D.__reversed__() -- return a reverse iterator over the deque """
pass

def __rmul__(self, *args, **kwargs): # real signature unknown
""" Return self*value. """
pass

def __setitem__(self, *args, **kwargs): # real signature unknown
""" Set self[key] to value. """
pass

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
deque源码

 ①append(self, *args, **kwargs)
 ②appendleft(self, *args, **kwargs)
 ③extend(self, *args, **kwargs) 多个元素一起添加(从右侧)
 ④extendleft(self, *args, **kwargs)   多个元素一起添加(从左侧)

from collections import deque
d=deque(['aaaa','ddddd','eeee'])
print(d)
d.extend(['111','222','333'])
print(d)
d.extendleft(['444','555'])
print(d)

输出

deque(['aaaa', 'ddddd', 'eeee'])
deque(['aaaa', 'ddddd', 'eeee', '111', '222', '333'])
deque(['555', '444', 'aaaa', 'ddddd', 'eeee', '111', '222', '333']) 

 ⑤pop(self, *args, **kwargs) 取元素
 ⑥popleft(self, *args, **kwargs)
 ⑦rotate(self, *args, **kwargs)  首尾连接,向左/向右移动元素

from collections import deque
d=deque(['555', '444', 'aaaa', 'ddddd', 'eeee', '111', '222', '333'])
print(d)
d.rotate(1)
print(d)
d.rotate(-1)
print(d)

输出

deque(['555', '444', 'aaaa', 'ddddd', 'eeee', '111', '222', '333'])
deque(['333', '555', '444', 'aaaa', 'ddddd', 'eeee', '111', '222'])
deque(['555', '444', 'aaaa', 'ddddd', 'eeee', '111', '222', '333'])

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: