Python基本数据类型(三)
一、set的函数说明
集合(set)是一个无序不重复元素的序列,基本功能是进行成员关系测试和删除重复元素,可以使用大括号({})或者 set()函数创建集合;
注:创建一个空集合必须用set()而不是{ },因为{ }是用来创建一个空字典;
在python中set提供的函数如下:
class set(object):
"""
set() -> 空的新集合对象;
set(iterable) -> 新的集合对象;
Build an unordered collection of unique elements.
"""
def add(self, *args, **kwargs): # real signature unknown
"""
在集合中增加元素,如果添加元素已存在于集合,则无效;
例如:
>>> x = set()
>>> x.add('x')
>>> x
set(['x'])
"""
pass
def clear(self, *args, **kwargs): # real signature unknown
"""
清空集合;
例如:
>>> x = set(['k1','k2'])
>>> x
set(['k2', 'k1'])
>>> x.clear()
>>> x
set([])
"""
pass
def copy(self, *args, **kwargs): # real signature unknown
"""
集合的浅拷贝;
例如:
>>> x = set(['k1','k2'])
>>> y = x.copy()
>>> y
set(['k2', 'k1'])
"""
pass
def difference(self, *args, **kwargs): # real signature unknown
"""
获取两个集合的不同(差集),并生成一个新的集合;
即获取x.difference(y)的差集,相当于获取x多余y的集合值;
如果x包含于y,则获取空值;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['a','b'])
>>> s3 = x.difference(y)
>>> s3
set(['c'])
>>> s4 = y.difference(x)
>>> s4
set([])
"""
pass
def difference_update(self, *args, **kwargs): # real signature unknown
"""
获取两个集合的不同(差集),改变原来的集合;
即获取x.difference_update(y)的差集,相当于获取x多余y的集合值,并重写进x;
如果x包含于y,则获取空值,并重写进x;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['a','b'])
>>> x.difference_update(y)
>>> x
set(['c'])
>>> y
set(['a', 'b'])
>>> x = set(['a','b','c'])
>>> y = set(['a','b'])
>>> y.difference_update(x)
>>> y
set([])
>>> x
set(['a', 'c', 'b'])
"""
pass
def discard(self, *args, **kwargs): # real signature unknown
"""
移除集合中的一个指定元素,如果这个元素不存在,则不变;
例如:
>>> x = set(['a','b','c'])
>>> x.discard('a')
>>> x
set(['c', 'b'])
>>> x.discard('d')
>>> x
set(['c', 'b'])
"""
pass
def intersection(self, *args, **kwargs): # real signature unknown
"""
获取两个集合的交集,生成一个新的集合;
即获取x.intersection(y)的交集,相当于获取x与y相等的那部分集合值;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['a','b'])
>>> x.intersection(y)
set(['a', 'b'])
>>> x
set(['a', 'c', 'b'])
>>> y
set(['a', 'b'])
"""
pass
def intersection_update(self, *args, **kwargs): # real signature unknown
"""
获取两个集合的交集,改变原来的集合;
即获取x.intersection_update(y)的交集,相当于获取x与y相等的那部分集合值,并重写进x;
如果x包含于y,则无变化;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['a','b'])
>>> x.intersection_update(y)
>>> x
set(['a', 'b'])
>>> x = set(['a','b','c'])
>>> y = set(['a','b'])
>>> y.intersection_update(x)
>>> y
set(['a', 'b'])
>>> x
set(['a', 'c', 'b'])
"""
pass
def isdisjoint(self, *args, **kwargs): # real signature unknown
"""
判断两个集合是否没有交集,如果是返回True,如果不是返回False;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['a','b'])
>>> x.isdisjoint(y)
False
>>> y = set(['d'])
>>> x.isdisjoint(y)
True
"""
pass
def issubset(self, *args, **kwargs): # real signature unknown
"""
判断一个集合是否是另一个集合的子集;
即x.issubset(y),相当于判断x是否y的子集;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['a','b'])
>>> x.issubset(y)
False
>>> y.issubset(x)
True
"""
pass
def issuperset(self, *args, **kwargs): # real signature unknown
"""
判断一个集合是否包含另一个集合;
即x.issuperset(y),相当于判断x是否包含y;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['a','b'])
>>> x.issuperset(y)
True
>>> y.issuperset(x)
False
"""
pass
def pop(self, *args, **kwargs): # real signature unknown
"""
删除并返回任意设置的元素,如果集合为空,则引发KeyError;
例如:
>>> x = set(['a','b','c'])
>>> x.pop()
'a'
>>> x.pop()
'c'
>>> x.pop()
'b'
>>> x.pop()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'pop from an empty set'
"""
pass
def remove(self, *args, **kwargs): # real signature unknown
"""
移除集合中指定的元素,如果集合为空或指定的元素不存在,则引发KeyError;
例如:
>>> x = set(['a','b','c'])
>>> x.remove('b')
>>> x
set(['a', 'c'])
>>> x.remove('d')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'd'
"""
pass
def symmetric_difference(self, *args, **kwargs): # real signature unknown
"""
把两个集合中的不同元素,即差集,放到一个新的集合中;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['c','d'])
>>> x.symmetric_difference(y)
set(['a', 'b', 'd'])
"""
pass
def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
"""
两个集合不相同的元素,即差集,并改变原集合;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['c','d'])
>>> x.symmetric_difference_update(y)
>>> x
set(['a', 'b', 'd'])
>>> y
set(['c', 'd'])
"""
pass
def uNIOn(self, *args, **kwargs): # real signature unknown
"""
获取两个集合的并集,并生成一个新的集合;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['c','d'])
>>> x.union(y)
set(['a', 'c', 'b', 'd'])
>>> x
set(['a', 'c', 'b'])
"""
pass
def update(self, *args, **kwargs): # real signature unknown
"""
获取两个集合的并集,并生改变原集合;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['c','d'])
>>> x.update(y)
>>> x
set(['a', 'c', 'b', 'd'])
>>> y
set(['c', 'd'])
"""
pass
def __and__(self, y): # real signature unknown; restored from __doc__
"""
x.__and__(y) 等同于 x&y
集合与操作,相当于获取两个集合相同值,即交集,并进行返回;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['c','d'])
>>> x.__and__(y)
set(['c'])
>>> x
set(['a', 'c', 'b'])
>>> y
set(['c', 'd'])
>>> x = set(['a','b','c'])
>>> y = set(['b','c'])
>>> x & y
set(['c', 'b'])
"""
pass
def __cmp__(self, y): # real signature unknown; restored from __doc__
"""
x.__cmp__(y) 等同于 cmp(x,y)
无意义 (Python2特有,python3已删除)
"""
pass
def __contains__(self, y): # real signature unknown; restored from __doc__
"""
x.__contains__(y) 等同于 y in x.
集合包含判断,即判断y是否包含在x中,返回布尔值;
例如:
>>> x = set(['a','b','c'])
>>> y = 'a'
>>> x.__contains__(y)
True
>>> y in x
True
>>> y = 'd'
>>> x.__contains__(y)
False
"""
pass
def __eq__(self, y): # real signature unknown; restored from __doc__
"""
x.__eq__(y) 等同于 x==y
集合等同于判断,即判断x是否等于y,返回布尔值;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['a','b','c'])
>>> x.__eq__(y)
True
>>> x == y
True
>>> y = set(['a','b'])
>>> x == y
False
"""
pass
def __getattribute__(self, name): # real signature unknown; restored from __doc__
"""
x.__getattribute__('name') 等同于 x.name
"""
pass
def __ge__(self, y): # real signature unknown; restored from __doc__
"""
x.__ge__(y) 等同于 x>=y
集合大小等于判断,返回布尔值;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['a','b'])
>>> x.__ge__(y)
True
>>> x >= y
True
>>> y = set(['a','b','c'])
>>> x >= y
True
>>> y = set(['a','b','c','d'])
>>> x >= y
False
"""
pass
def __gt__(self, y): # real signature unknown; restored from __doc__
"""
x.__gt__(y) 等同于 x>y
集合大于判断,返回布尔值;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['a','b'])
>>> x.__gt__(y)
True
>>> x > y
True
>>> y = set(['a','b','c'])
>>> x > y
False
"""
pass
def __iand__(self, y): # real signature unknown; restored from __doc__
"""
x.__iand__(y) 等同于 x&=y
集合与操作,相当于获取两个集合相同值,即交集,并进行返回及修改集合x;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['c','d'])
>>> x.__iand__(y)
set(['c'])
>>> x
set(['c'])
>>> y
set(['c', 'd'])
>>> x = set(['a','b','c'])
"""
pass
def __init__(self, seq=()): # known special case of set.__init__
"""
set() -> new empty set object
set(iterable) -> new set object
Build an unordered collection of unique elements.
# (copied from class doc)
构造方法,执行x = set()时自动调用集合函数;
"""
pass
def __ior__(self, y): # real signature unknown; restored from __doc__
"""
x.__ior__(y) 等同于 x|=y
获取两个集合的并集,并进行返回及改变原集合;
>>> x = set(['a','b','c'])
>>> y = set(['c','d'])
>>> x.__ior__(y)
set(['a', 'c', 'b', 'd'])
>>> x
set(['a', 'c', 'b', 'd'])
>>> y
set(['c', 'd'])
>>> x = set(['a','b','c'])
>>> x |= y
set(['a', 'c', 'b', 'd'])
>>> x
set(['a', 'c', 'b', 'd'])
>>> y
set(['c', 'd'])
"""
pass
def __isub__(self, y): # real signature unknown; restored from __doc__
"""
x.__isub__(y) 等同于 x-=y
集合减法,即x集合减去y集合,并进行结果返回及修改x集合;
例如:
>>> x = set(['a','b','c','d'])
>>> y = set(['c','d'])
>>> x.__isub__(y)
set(['a', 'b'])
>>> x
set(['a', 'b'])
>>> y
set(['c', 'd'])
"""
pass
def __iter__(self): # real signature unknown; restored from __doc__
"""
x.__iter__() 等同于 iter(x)
迭代对象,返回自己;
"""
pass
def __ixor__(self, y): # real signature unknown; restored from __doc__
"""
x.__ixor__(y) 等同于 x^=y
把两个集合中的不同元素(对称差集)放到一个原集合中,即把x与y集合的不同元素,放置到x集合中;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['c','d'])
>>> x.__ixor__(y)
set(['a', 'b', 'd'])
>>> x
set(['a', 'b', 'd'])
>>> y
set(['c', 'd'])
>>> x = set(['a','b','c'])
>>> x ^= y
set(['a', 'b', 'd'])
>>> x
set(['a', 'b', 'd'])
>>> y
set(['c', 'd'])
"""
pass
def __len__(self): # real signature unknown; restored from __doc__
"""
x.__len__() 等同于 len(x)
返回集合长度;
"""
pass
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<y
集合小于判断,返回布尔值;
"""
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 __or__(self, y): # real signature unknown; restored from __doc__
"""
x.__or__(y) 等同于 x|y
获取两个集合的并集,并生成一个新的集合;
例如:
>>> x = set(['a','b','c'])
>>> y = set(['c','d'])
>>> x.__or__(y)
set(['a', 'c', 'b', 'd'])
>>> x
set(['a', 'c', 'b'])
>>> y
set(['c', 'd'])
>>> y | x
set(['a', 'c', 'b', 'd'])
>>> x
set(['a', 'c', 'b'])
>>> y
set(['c', 'd'])
"""
pass
def __rand__(self, y): # real signature unknown; restored from __doc__
"""
x.__rand__(y) 等同于 y&x
获取两个集合的交集,生成一个新的集合;
例如:
>>> x = set(['a','b'])
>>> y = set(['a','b','c'])
>>> x.__rand__(y)
set(['a', 'b'])
>>> y & x
set(['a', 'b'])
"""
pass
def __reduce__(self, *args, **kwargs): # real signature unknown
"""
Return state infORMation for pickling.
"""
pass
def __repr__(self): # real signature unknown; restored from __doc__
"""
x.__repr__() 等同于 repr(x)
转化为解释器可读取的形式,即转换为字符串格式;
"""
pass
def __ror__(self, y): # real signature unknown; restored from __doc__
"""
x.__ror__(y) 等同于 y|x
获取两个集合的并集,并生成一个新的集合;;
例如:
>>> x = set(['a','b'])
>>> y = set(['a','b','c'])
>>> x.__ror__(y)
set(['a', 'c', 'b'])
>>> x
set(['a', 'b'])
>>> y
set(['a', 'c', 'b'])
>>> y | x
set(['a', 'c', 'b'])
"""
pass
def __rsub__(self, y): # real signature unknown; restored from __doc__
"""
x.__rsub__(y) 等同于 y-x
获取两个集合的不同(差集),并生成一个新的集合(项在y中,但不在x中);
例如:
>>> x = set(['a','b'])
>>> y = set(['a','b','c'])
>>> x.__rsub__(y)
set(['c'])
>>> y.__rsub__(x)
set([])
"""
pass
def __rxor__(self, y): # real signature unknown; restored from __doc__
"""
x.__rxor__(y) 等同于 y^x
获取两个集合的不同(差集),并生成一个新的集合
例如:
>>> x = set(['a','b','c'])
>>> y = set(['c','d'])
>>> x.__rxor__(y)
set(['a', 'b', 'd'])
>>> x
set(['a', 'c', 'b'])
>>> y
set(['c', 'd'])
>>> y ^ x
set(['a', 'b', 'd'])
"""
pass
def __sizeof__(self): # real signature unknown; restored from __doc__
"""
S.__sizeof__() -> size of S in memory, in bytes
返回内存中的大小(以字节为单位);
"""
pass
def __sub__(self, y): # real signature unknown; restored from __doc__
"""
x.__sub__(y) 等同于 x-y
获取两个集合的不同(差集),并生成一个新的集合(项在x中,但不在y中);
例如:
>>> x = set(['a','b','c'])
>>> y = set(['c','d'])
>>> x.__sub__(y)
set(['a', 'b'])
>>> x
set(['a', 'c', 'b'])
>>> y
set(['c', 'd'])
>>> y - x
set(['d'])
"""
pass
def __xor__(self, y): # real signature unknown; restored from __doc__
"""
x.__xor__(y) 等同于 x^y
两个集合不相同的元素(差集),并返回结果;
>>> x = set(['a','b','c'])
>>> y = set(['c','d'])
>>> x.__xor__(y)
set(['a', 'b', 'd'])
>>> x
set(['a', 'c', 'b'])
>>> x
set(['c', 'd'])
>>> y ^ x
set(['a', 'b', 'd'])
"""
pass
__hash__ = None
二、collection系列函数说明
collections模块自Python 2.4版本开始被引入,包含了dict、set、list、tuple以外的一些特殊的容器类型,分别是:
OrderedDict类:排序字典,是字典的子类。引入自2.7;
namedtuple()函数:命名元组,是一个工厂函数。引入自2.6;
Counter类:为hashable对象计数,是字典的子类。引入自2.7;
deque:双向队列。引入自2.4;
defaultdict:使用工厂函数创建字典,使不用考虑缺失的字典键。引入自2.5;
使用的时候需要用import导入collections模块;
1、计数器(counter)函数说明
Counter类的目的是用来跟踪值出现的次数。它是一个无序的容器类型,以字典的键值对形式存储,其中元素作为key,其计数作为value。计数值可以是任意的Interger(包括0和负数);
注:具备字典的所有功能 + 自己的功能;
########################################################################
### Counter
########################################################################
def _count_elements(mapping, iterable):
'Tally elements from the iterable.'
mapping_get = mapping.get
for elem in iterable:
mapping[elem] = mapping_get(elem, 0) + 1
try: # Load C helper function if available
from _collections import _count_elements
except ImportError:
pass
'''
如果C的帮助函数可用的话,则加载; (Python3新增)
'''
class Counter(dict):
'''
Dict子类用于计算哈希项,有时称为包或多集,元素存储为字典键,它们的计数存储为字典值;
>>> c = Counter('abcdeabcdabcaba') # count elements from a string
>>> c.most_common(3) # three most common elements
[('a', 5), ('b', 4), ('c', 3)]
>>> sorted(c) # list all unique elements
['a', 'b', 'c', 'd', 'e']
>>> ''.join(sorted(c.elements())) # list elements with repetitions
'aaaaabbbbcccdde'
>>> sum(c.values()) # total of all counts
15
>>> c['a'] # count of letter 'a'
5
>>> for elem in 'shazam': # update counts from an iterable
... c[elem] += 1 # by adding 1 to each element's count
>>> c['a'] # now there are seven 'a'
7
>>> del c['b'] # remove all 'b'
>>> c['b'] # now there are zero 'b'
0
>>> d = Counter('simsalabim') # make another counter
>>> c.update(d) # add in the second counter
>>> c['a'] # now there are nine 'a'
9
>>> c.clear() # empty the counter
>>> c
Counter()
Note: If a count is set to zero or reduced to zero, it will remain
in the counter until the entry is deleted or the counter is cleared:
>>> c = Counter('aaabbc')
>>> c['b'] -= 2 # reduce the count of 'b' by two
>>> c.most_common() # 'b' is still in, but its count is zero
[('a', 3), ('c', 1), ('b', 0)]
'''
# References:
# Http://en.wikipedia.org/wiki/Multiset
# http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html
# http://www.demo2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm
# http://code.activestate.com/recipes/259174/
# Knuth, TAOCP Vol. II section 4.6.3
def __init__(*args, **kwds):
'''
创建一个新的空Counter对象,可对输入可迭代元素进行计数,也可以对另外一个元素映射过来的元素进行计数;
主要是先调用父类(dict)的初始化,然后使用update函数来更新参数;
>>> c = Counter() # a new, empty counter
>>> c = Counter('gallahad') # a new counter from an iterable
>>> c = Counter({'a': 4, 'b': 2}) # a new counter from a mapping
>>> c = Counter(a=4, b=2) # a new counter from keyWord args
'''
if not args:
raise TypeError("descriptor '__init__' of 'Counter' object "
"needs an argument")
self, *args = args
if len(args) > 1:
raise TypeError('expected at most 1 arguments, Got %d' % len(args))
super(Counter, self).__init__()
self.update(*args, **kwds)
def __missing__(self, key):
'The count of elements not in the Counter is zero.'
# Needed so that self[missing_item] does not raise KeyError
return 0
'''
对于不存在的元素,返回计数器为0;
总结一下就是dict本身没有这个方法,但是如果当前类为dict的子类的话;
会在缺失的情况下查看有没有实现__missing__方法,如果有的话,就返回__miss__方法的值;
所以Counter作为dict的子类实现了__missing__方法,在缺失的时候返回0;
这也就是为什么在Counter类中,如果找不到key,会返回0而不是产生一个KeyError;
例如:
>>> import collections
>>> c = collections.Counter('abbcc')
>>> c['a']
2
>>> c['b']
2
>>> c['d']
0
'''
def most_common(self, n=None):
'''List the n most common elements and their counts from the most
common to the least. If n is None, then list all element counts.
>>> Counter('abcdeabcdabcaba').most_common(3)
[('a', 5), ('b', 4), ('c', 3)]
'''
# Emulate Bag.sortedByCount from Smalltalk
if n is None:
return sorted(self.items(), key=_itemgetter(1), reverse=True)
return _heapq.nlargest(n, self.items(), key=_itemgetter(1))
'''
数量从大到写排列,返回一个TopN列表,如果n没有被指定,则返回所有元素;
当多个元素计数值相同时,按照字母序排列;
例如:
>>> Counter('abcdeabcdabcaba').most_common(3)
[('a', 5), ('b', 4), ('c', 3)]
'''
def elements(self):
'''Iterator over elements repeating each as many times as its count.
>>> c = Counter('ABCABC')
>>> sorted(c.elements())
['A', 'A', 'B', 'B', 'C', 'C']
# Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
>>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
>>> product = 1
>>> for factor in prime_factors.elements(): # loop over factors
... product *= factor # and multiply them
>>> product
1836
Note, if an element's count has been set to zero or is a negative
number, elements() will ignore it.
'''
# Emulate Bag.do from Smalltalk and Multiset.begin from c++.
return _chain.from_iterable(_starmap(_repeat, self.items()))
'''
返回一个迭代器。元素被重复了多少次,在该迭代器中就包含多少个该元素;
所有元素按照字母序排序,个数小于1的元素不被包含;
注:此处非所有元素集合,而是包含所有元素集合的迭代器;
例如:
>>> import collections
>>> c = collections.Counter(a=4, b=2, c=0, d=-2)
>>> list(c.elements())
['a', 'a', 'a', 'a', 'b', 'b']
'''
# Override dict methods where necessary
@claSSMethod
def fromkeys(cls, iterable, v=None):
# There is no equivalent method for counters because setting v=1
# means that no element can have a count greater than one.
raise NotImplementedError(
'Counter.fromkeys() is undefined. Use Counter(iterable) instead.')
'''
未实现的类方法;
'''
def update(*args, **kwds):
'''Like dict.update() but add counts instead of replacing them.
Source can be an iterable, a dictionary, or another Counter instance.
>>> c = Counter('which')
>>> c.update('witch') # add elements from another iterable
>>> d = Counter('watch')
>>> c.update(d) # add elements from another counter
>>> c['h'] # four 'h' in which, witch, and watch
4
'''
# The regular dict.update() operation makes no sense here because the
# replace behavior results in the some of original untouched counts
# being mixed-in with all of the other counts for a mismash that
# doesn't have a straight-forward interpretation in most counting
# contexts. Instead, we implement straight-addition. Both the inputs
# and outputs are allowed to contain zero and negative counts.
if not args:
raise TypeError("descriptor 'update' of 'Counter' object "
"needs an argument")
self, *args = args
if len(args) > 1:
raise TypeError('expected at most 1 arguments, got %d' % len(args))
iterable = args[0] if args else None
if iterable is not None:
if isinstance(iterable, Mapping):
if self:
self_get = self.get
for elem, count in iterable.items():
self[elem] = count + self_get(elem, 0)
else:
super(Counter, self).update(iterable) # fast path when counter is empty
else:
_count_elements(self, iterable)
if kwds:
self.update(kwds)
'''
更新计数器,其实就是增加;如果原来没有,则新建,如果有则加一;
例如:
>>> from collections import Counter
>>> c = Counter('which')
>>> c
Counter({'h': 2, 'i': 1, 'c': 1, 'w': 1})
>>> c.update('witch')
>>> c
Counter({'h': 3, 'i': 2, 'c': 2, 'w': 2, 't': 1})
>>> c['h']
3
>>> d = Counter('watch')
>>> d
Counter({'a': 1, 'h': 1, 'c': 1, 't': 1, 'w': 1})
>>> c.update(d)
>>> c
Counter({'h': 4, 'c': 3, 'w': 3, 'i': 2, 't': 2, 'a': 1})
>>> c['h']
4
'''
def subtract(*args, **kwds):
'''Like dict.update() but subtracts counts instead of replacing them.
Counts can be reduced below zero. Both the inputs and outputs are
allowed to contain zero and negative counts.
Source can be an iterable, a dictionary, or another Counter instance.
>>> c = Counter('which')
>>> c.subtract('witch') # subtract elements from another iterable
>>> c.subtract(Counter('watch')) # subtract elements from another counter
>>> c['h'] # 2 in which, minus 1 in witch, minus 1 in watch
0
>>> c['w'] # 1 in which, minus 1 in witch, minus 1 in watch
-1
'''
if not args:
raise TypeError("descriptor 'subtract' of 'Counter' object "
"needs an argument")
self, *args = args
if len(args) > 1:
raise TypeError('expected at most 1 arguments, got %d' % len(args))
iterable = args[0] if args else None
if iterable is not None:
self_get = self.get
if isinstance(iterable, Mapping):
for elem, count in iterable.items():
self[elem] = self_get(elem, 0) - count
else:
for elem in iterable:
self[elem] = self_get(elem, 0) - 1
if kwds:
self.subtract(kwds)
'''
相减,原来的计数器中的每一个元素的数量减去后添加的元素的数量;
例如:
>>> from collections import Counter
>>> c = Counter('which')
>>> c.subtract('witch')
>>> c
Counter({'h': 1, 'i': 0, 'c': 0, 'w': 0, 't': -1})
>>> c['h']
1
>>> d = Counter('watch')
>>> c.subtract(d)
>>> c['a']
-1
'''
def copy(self):
'Return a shallow copy.'
return self.__class__(self)
'''
浅拷贝;
例如:
>>> from collections import Counter
>>> c = Counter('abcdcba')
>>> c
Counter({'a': 2, 'c': 2, 'b': 2, 'd': 1})
>>> d = c.copy()
>>> d
Counter({'a': 2, 'c': 2, 'b': 2, 'd': 1})
'''
def __reduce__(self):
return self.__class__, (dict(self),)
'''
返回一个元组(类型,元组);
例如:
>>> c = Counter('abcdcba')
>>> c.__reduce__()
(<class 'collections.Counter'>, ({'a': 2, 'c': 2, 'b': 2, 'd': 1},))
>>> d = c.__reduce__()
>>> type(d)
<type 'tuple'>
'''
def __delitem__(self, elem):
'Like dict.__delitem__() but does not raise KeyError for missing values.'
if elem in self:
super().__delitem__(elem)
'''
删除元素,等同于del;
本质上就是一个不抛出KeyError的dict类的__delitem()__;
>>> c = Counter('abcdcba')
>>> c
Counter({'a': 2, 'c': 2, 'b': 2, 'd': 1})
>>> c['b'] = 0
>>> c
Counter({'a': 2, 'c': 2, 'd': 1, 'b': 0})
>>> c.__delitem__('a')
>>> c
Counter({'c': 2, 'd': 1, 'b': 0})
>>> del c['b']
>>> c
Counter({'c': 2, 'd': 1})
'''
def __repr__(self):
if not self:
return '%s()' % self.__class__.__name__
try:
items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
return '%s({%s})' % (self.__class__.__name__, items)
except TypeError:
# handle case where values are not orderable
return '{0}({1!r})'.format(self.__class__.__name__, dict(self))
'''
如果没有对象就返回类的名字,否则返回类的名字并且返回利用most_common()方法得到类中的信息;
例如:
>>> from collections import Counter
>>> c = Counter('aabbccdd')
>>> c.__repr__()
"Counter({'a': 2, 'c': 2, 'b': 2, 'd': 2})"
>>> c = Counter()
>>> c.__repr__()
'Counter()'
'''
# Multiset-style mathematical operations discussed in:
# Knuth TAOCP Volume II section 4.6.3 exercise 19
# and at http://en.wikipedia.org/wiki/Multiset
#
# Outputs guaranteed to only include positive counts.
#
# To strip negative and zero counts, add-in an empty counter:
# c += Counter()
def __add__(self, other):
'''Add counts from two counters.
>>> Counter('abbb') + Counter('bcc')
Counter({'b': 4, 'c': 2, 'a': 1})
'''
if not isinstance(other, Counter):
return NotImplemented
result = Counter()
for elem, count in self.items():
newcount = count + other[elem]
if newcount > 0:
result[elem] = newcount
for elem, count in other.items():
if elem not in self and count > 0:
result[elem] = count
return result
'''
加法运算,相当于+,结果中只会出现计数count大于0的元素;
例如:
>>> c1 = Counter({'a':0,'b':1,'c':3})
>>> c2 = Counter({'b':2,'c':1})
>>> c1.__add__(c2)
Counter({'c': 4, 'b': 3})
>>> c1 + c2
Counter({'c': 4, 'b': 3})
'''
def __sub__(self, other):
''' Subtract count, but keep only results with positive counts.
>>> Counter('abbbc') - Counter('bccd')
Counter({'b': 2, 'a': 1})
'''
if not isinstance(other, Counter):
return NotImplemented
result = Counter()
for elem, count in self.items():
newcount = count - other[elem]
if newcount > 0:
result[elem] = newcount
for elem, count in other.items():
if elem not in self and count < 0:
result[elem] = 0 - count
return result
'''
减法运算,相当于-,结果中只会出现计数count大于0的元素;
例如:
>>> c1 = Counter({'a':0,'b':1,'c':3})
>>> c2 = Counter({'b':2,'c':1})
>>> c1.__sub__(c2)
Counter({'c': 2})
>>> c1 - c2
Counter({'c': 2})
'''
def __or__(self, other):
'''Union is the maximum of value in either of the input counters.
>>> Counter('abbb') | Counter('bcc')
Counter({'b': 3, 'c': 2, 'a': 1})
'''
if not isinstance(other, Counter):
return NotImplemented
result = Counter()
for elem, count in self.items():
other_count = other[elem]
newcount = other_count if count < other_count else count
if newcount > 0:
result[elem] = newcount
for elem, count in other.items():
if elem not in self and count > 0:
result[elem] = count
return result
'''
并集运算,相当于|,结果中只会出现计数count大于0的元素及主要是选相同元素中count最大的一个;
例如:
>>> c1 = Counter({'a':0,'b':1,'c':3})
>>> c2 = Counter({'b':2,'c':1})
>>> c1.__or__(c2)
Counter({'c': 3, 'b': 2})
>>> c1 | c2
Counter({'c': 3, 'b': 2})
'''
def __and__(self, other):
''' Intersection is the minimum of corresponding counts.
>>> Counter('abbb') & Counter('bcc')
Counter({'b': 1})
'''
if not isinstance(other, Counter):
return NotImplemented
result = Counter()
for elem, count in self.items():
other_count = other[elem]
newcount = count if count < other_count else other_count
if newcount > 0:
result[elem] = newcount
return result
'''
交集运算,相当于&,结果中只会出现计数count大于0的元素及主要是选相同元素中count最小的一个;
例如:
>>> c1 = Counter({'a':0,'b':1,'c':3})
>>> c2 = Counter({'b':2,'c':1})
>>> c1.__and__(c2)
Counter({'c': 1, 'b': 1})
>>> c1 & c2
Counter({'c': 1, 'b': 1})
'''
def __pos__(self):
'Adds an empty counter, effectively stripping negative and zero counts'
result = Counter()
for elem, count in self.items():
if count > 0:
result[elem] = count
return result
'''
用于清除值为负数和零的计数; (Python3新增)
例如:
>>> from collections import Counter
>>> c1 = Counter({'a':0,'b':1,'c':-3})
>>> c1.__pos__()
Counter({'b': 1})
'''
def __neg__(self):
'''Subtracts from an empty counter. Strips positive and zero counts,
and flips the sign on negative counts.
'''
result = Counter()
for elem, count in self.items():
if count < 0:
result[elem] = 0 - count
return result
'''
用于清除值为正数或者零的计数,并将值为负数的计数,转换为正数; (Python3新增)
例如:
>>> c1 = Counter({'a':0,'b':1,'c':-3})
>>> c1.__neg__()
Counter({'c': 3})
'''
def _keep_positive(self):
'''Internal method to strip elements with a negative or zero count'''
nonpositive = [elem for elem, count in self.items() if not count > 0]
for elem in nonpositive:
del self[elem]
return self
def __iadd__(self, other):
'''Inplace add from another counter, keeping only positive counts.
>>> c = Counter('abbb')
>>> c += Counter('bcc')
>>> c
Counter({'b': 4, 'c': 2, 'a': 1})
'''
for elem, count in other.items():
self[elem] += count
return self._keep_positive()
'''
自加,相当于+=,结果中只会出现计数count大于0的元素; (Python3新增)
例如:
>>> c1 = Counter({'a':0,'b':1,'c':3})
>>> c1 += Counter({'b':2,'c':1})
>>> c1
Counter({'c': 4, 'b': 3})
'''
def __isub__(self, other):
'''Inplace subtract counter, but keep only results with positive counts.
>>> c = Counter('abbbc')
>>> c -= Counter('bccd')
>>> c
Counter({'b': 2, 'a': 1})
'''
for elem, count in other.items():
self[elem] -= count
return self._keep_positive()
'''
自减,相当于-=,结果中只会出现计数count大于0的元素; (Python3新增)
例如:
>>> c1 = Counter({'a':0,'b':1,'c':3})
>>> c1 -= Counter({'b':1,'c':1})
>>> c1
Counter({'c': 2})
'''
def __ior__(self, other):
'''Inplace union is the maximum of value from either counter.
>>> c = Counter('abbb')
>>> c |= Counter('bcc')
>>> c
Counter({'b': 3, 'c': 2, 'a': 1})
'''
for elem, other_count in other.items():
count = self[elem]
if other_count > count:
self[elem] = other_count
return self._keep_positive()
'''
自并集运算,相当于|=,结果中只会出现计数count大于0的元素及主要是选相同元素中count最大的一个; (Python3新增)
例如:
>>> c1 = Counter({'a':0,'b':1,'c':3})
>>> c1 |= Counter({'b':1,'d':2})
>>> c1
Counter({'c': 3, 'd': 2, 'b': 1})
'''
def __iand__(self, other):
'''Inplace intersection is the minimum of corresponding counts.
>>> c = Counter('abbb')
>>> c &= Counter('bcc')
>>> c
Counter({'b': 1})
'''
for elem, count in self.items():
other_count = other[elem]
if other_count < count:
self[elem] = other_count
return self._keep_positive()
'''
自交集运算,相当于&=,结果中只会出现计数count大于0的元素及主要是选相同元素中count最小的一个; (Python3新增)
例如:
>>> c1 = Counter({'a':0,'b':1,'c':3})
>>> c1 &= Counter({'b':1,'d':2})
>>> c1
Counter({'b': 1})
'''
2、有序字典(OrderdDict)函数说明
OrderdDict是对字典类型的补充,他记住了字典元素添加的顺序;
################################################################################
### OrderedDict
################################################################################
class _OrderedDicTKEysView(KeysView):
def __reversed__(self):
yield from reversed(self._mapping)
'''
用于被OrderedDict的keys方法调用; (Python3新增)
'''
class _OrderedDictItemsView(ItemsView):
def __reversed__(self):
for key in reversed(self._mapping):
yield (key, self._mapping[key])
'''
用于被OrderedDict的items方法调用; (Python3新增)
'''
class _OrderedDictValuesView(ValuesView):
def __reversed__(self):
for key in reversed(self._mapping):
yield self._mapping[key]
'''
用于被OrderedDict的values方法调用; (Python3新增)
'''
class _Link(object):
__slots__ = 'prev', 'next', 'key', '__weakref__'
'''
未实现的方法; (Python3新增)
'''
class OrderedDict(dict):
'Dictionary that remembers insertion order'
# An inherited dict maps keys to values.
# The inherited dict provides __getitem__, __len__, __contains__, and get.
# The remaining methods are order-aware.
# Big-O running times for all methods are the same as regular dictionaries.
# The internal self.__map dict maps keys to links in a doubly linked list.
# The circular doubly linked list starts and ends with a sentinel element.
# The sentinel element never gets deleted (this simplifies the algorithm).
# The sentinel is in self.__hardroot with a weakref proxy in self.__root.
# The prev links are weakref proxies (to prevent circular references).
# Individual links are kept alive by the hard reference in self.__map.
# Those hard references disappear when a key is deleted from an OrderedDict.
'''
记住插入顺序的字典;
继承的dict的keys和values;
继承的dict提供__getitem__,__len__,__contains__和get方法;
其余的方法都按顺序执行;
所有方法的执行时间都和普通字典一样;
引用在OrderedDict删除键时消失;
'''
def __init__(*args, **kwds):
'''
初始化有序字典。签名与常规字典相同,但不推荐使用关键字参数,因为插入顺序;
'''
if not args:
raise TypeError("descriptor '__init__' of 'OrderedDict' object "
"needs an argument")
self, *args = args
if len(args) > 1:
raise TypeError('expected at most 1 arguments, got %d' % len(args))
try:
self.__root
except AttributeError:
self.__hardroot = _Link()
self.__root = root = _proxy(self.__hardroot)
root.prev = root.next = root
self.__map = {}
self.__update(*args, **kwds)
def __setitem__(self, key, value,
dict_setitem=dict.__setitem__, proxy=_proxy, Link=_Link):
'od.__setitem__(i, y) <==> od[i]=y'
# Setting a new item creates a new link at the end of the linked list,
# and the inherited dictionary is updated with the new key/value pair.
if key not in self:
self.__map[key] = link = Link()
root = self.__root
last = root.prev
link.prev, link.next, link.key = last, root, key
last.next = link
root.prev = proxy(link)
dict_setitem(self, key, value)
'''
od.__setitem__(i, y)等同于od[i]=y;
设置的新项目会在链接列表的末尾创建一个新链接,并且继承的字典使用新的键/值对进行更新方法;
例如:
>>> from collections import OrderedDict
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3'})
>>> od
OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
>>> od.__setitem__('k4','v4')
>>> od
OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3'), ('k4', 'v4')])
>>> od['k5'] = 'v5'
>>> od
OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3'), ('k4', 'v4'), ('k5', 'v5')])
'''
def __delitem__(self, key, dict_delitem=dict.__delitem__):
'od.__delitem__(y) <==> del od[y]'
# Deleting an existing item uses self.__map to find the link which gets
# removed by updating the links in the predecessor and successor nodes.
dict_delitem(self, key)
link = self.__map.pop(key)
link_prev = link.prev
link_next = link.next
link_prev.next = link_next
link_next.prev = link_prev
link.prev = None
link.next = None
'''
od.__delitem__(y)等同于del od[y];
使用self.__map找到现有项目并进行删除,删除后通过更新前导节点和后继节点的链接来覆盖删除的链接;
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3'})
>>> od.__delitem__('k1')
>>> od
OrderedDict([('k2', 'v2'), ('k3', 'v3')])
>>> del od['k2']
>>> od
OrderedDict([('k3', 'v3')])
'''
def __iter__(self):
'od.__iter__() <==> iter(od)'
# Traverse the linked list in order.
root = self.__root
curr = root.next
while curr is not root:
yield curr.key
curr = curr.next
'''
od.__iter__()等同于iter(od)
按顺序遍历字典链表;
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3'})
>>> od
OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
>>> od.__iter__()
<odict_iterator object at 0x0000027FF1D11F10>
>>> nod = od.__iter__()
>>> type(nod)
<class 'odict_iterator'>
>>> iter(od)
<odict_iterator object at 0x0000027FF1D11F10>
'''
def __reversed__(self):
'od.__reversed__() <==> reversed(od)'
# Traverse the linked list in reverse order.
root = self.__root
curr = root.prev
while curr is not root:
yield curr.key
curr = curr.prev
'''
od.__reversed__()等同于reversed(od)
以相反的顺序遍历字典链表,及返回一个反向迭代器;
'''
def clear(self):
'od.clear() -> None. Remove all items from od.'
root = self.__root
root.prev = root.next = root
self.__map.clear()
dict.clear(self)
'''
删除所有项目;
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3'})
>>> od
OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
>>> od.clear()
>>> od
OrderedDict()
'''
def popitem(self, last=True):
'''od.popitem() -> (k, v), return and remove a (key, value) pair.
Pairs are returned in LIFO order if last is true or FIFO order if false.
'''
if not self:
raise KeyError('dictionary is empty')
root = self.__root
if last:
link = root.prev
link_prev = link.prev
link_prev.next = root
root.prev = link_prev
else:
link = root.next
link_next = link.next
root.next = link_next
link_next.prev = root
key = link.key
del self.__map[key]
value = dict.pop(self, key)
return key, value
'''
按照先进先出删除key,value,并返回删除key和value;
如果参数last默认值True,表示以LIFO顺序(先进先出)进行删除和返回;
如果last为Flase,则以FIFO顺序(后进先出)进行删除和返回;
也可直接指定key的索引值进行删除;
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3','k4':'v4'})
>>> od.popitem()
('k4', 'v4')
>>> od
OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
>>> od.popitem(last = False)
('k1', 'v1')
>>> od
OrderedDict([('k2', 'v2'), ('k3', 'v3')])
>>> od.popitem(0)
('k2', 'v2')
>>> od
OrderedDict([('k3', 'v3')])
'''
def move_to_end(self, key, last=True):
'''Move an existing element to the end (or beginning if last==False).
Raises KeyError if the element does not exist.
When last=True, acts like a fast version of self[key]=self.pop(key).
'''
link = self.__map[key]
link_prev = link.prev
link_next = link.next
link_prev.next = link_next
link_next.prev = link_prev
root = self.__root
if last:
last = root.prev
link.prev = last
link.next = root
last.next = root.prev = link
else:
first = root.next
link.prev = root
link.next = first
root.next = first.prev = link
'''
移动现有元素,等同于od[key] = od.pop(key); (Python3新增)
当last参数为True(默认值)时,将现有元素移动到结尾;
如果last参数为False时,则将现有元素移动开头;
如果元素不存在,则引发KetError;
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3','k4':'v4'})
>>> od.move_to_end('k1')
>>> od
OrderedDict([('k2', 'v2'), ('k3', 'v3'), ('k4', 'v4'), ('k1', 'v1')])
>>> od.move_to_end('k4',last = False)
>>> od
OrderedDict([('k4', 'v4'), ('k2', 'v2'), ('k3', 'v3'), ('k1', 'v1')])
>>> od['k2'] = od.pop('k2')
>>> od
OrderedDict([('k4', 'v4'), ('k3', 'v3'), ('k1', 'v1'), ('k2', 'v2')])
'''
def __sizeof__(self):
sizeof = _sys.getsizeof
n = len(self) + 1 # number of links including root
size = sizeof(self.__dict__) # instance dictionary
size += sizeof(self.__map) * 2 # internal dict and inherited dict
size += sizeof(self.__hardroot) * n # link objects
size += sizeof(self.__root) * n # proxy objects
return size
'''
返回内存中的大小(以字节为单位); (Python3新增)
'''
update = __update = MutableMapping.update
def keys(self):
"D.keys() -> a set-like object providing a view on D's keys"
return _OrderedDictKeysView(self)
'''
返回一个包含key的类似集合的对象;
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3','k4':'v4'})
>>> od.keys()
odict_keys(['k1', 'k2', 'k3', 'k4'])
'''
def items(self):
"D.items() -> a set-like object providing a view on D's items"
return _OrderedDictItemsView(self)
'''
返回一个包含所有(key, value)类似集合的对象;
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3','k4':'v4'})
>>> od.items()
odict_items([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3'), ('k4', 'v4')])
'''
def values(self):
"D.values() -> an object providing a view on D's values"
return _OrderedDictValuesView(self)
'''
返回一个包含value的类似集合的对象;
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3','k4':'v4'})
>>> od.values()
odict_values(['v1', 'v2', 'v3', 'v4'])
'''
def iterkeys(self):
'od.iterkeys() -> an iterator over the keys in od'
return iter(self)
'''
key可迭代; (Python2特有,Python3已删除)
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3','k4':'v4'})
>>> od.iterkeys()
<generator object __iter__ at 0x02E653A0>
>>> nod = od.iterkeys()
>>> type(nod)
<type 'generator'>
>>> nod.next()
'k3'
>>> nod.next()
'k2'
>>> nod.next()
'k1'
'''
def itervalues(self):
'od.itervalues -> an iterator over the values in od'
for k in self:
yield self[k]
'''
value可迭代; (Python2特有,Python3已删除)
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3','k4':'v4'})
>>>od.itervalues()
<generator object itervalues at 0x02E654E0>
>>> nod = od.itervalues()
>>> type(nod)
<type 'generator'>
>>> nod.next()
'v3'
>>> nod.next()
'v2'
>>> nod.next()
'v1'
'''
def iteritems(self):
'od.iteritems -> an iterator over the (key, value) pairs in od'
for k in self:
yield (k, self[k])
'''
key, value可迭代; (Python2特有,Python3已删除)
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3','k4':'v4'})
>>> od.iteritems()
<generator object iteritems at 0x02E654E0>
>>> nod = od.iteritems()
>>> nod.next()
('k3', 'v3')
>>> nod.next()
('k2', 'v2')
>>> nod.next()
('k1', 'v1')
'''
__ne__ = MutableMapping.__ne__
__marker = object()
def pop(self, key, default=__marker):
'''od.pop(k[,d]) -> v, remove specified key and return the corresponding
value. If key is not found, d is returned if given, otherwise KeyError
is raised.
'''
if key in self:
result = self[key]
del self[key]
return result
if default is self.__marker:
raise KeyError(key)
return default
'''
删除指定的键并返回相应的值,如果设置了d参数,并未找到key时,则返回d参数,否则返回KeyError;
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3'})
>>> od.pop('k1')
'v1'
>>> od
OrderedDict([('k2', 'v2'), ('k3', 'v3')])
>>> od.pop('k4')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'k4'
>>> od.pop('k4','k4_no_found')
'k4_no_found'
'''
def setdefault(self, key, default=None):
'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od'
if key in self:
return self[key]
self[key] = default
return default
'''
设置key键,如果已存在key键,则不改变key键,并返回原有key键的value值,如果不存在Key键,由为它赋值;
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3'})
>>> od.setdefault('k3','v4')
'v3'
>>> od
OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
>>> od.setdefault('k4','v4')
'v4'
>>> od
OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3'), ('k4', 'v4')])
'''
@_recursive_repr()
def __repr__(self):
'od.__repr__() <==> repr(od)'
if not self:
return '%s()' % (self.__class__.__name__,)
return '%s(%r)' % (self.__class__.__name__, list(self.items()))
'''
od.__repr__()等同于repr(od)
转化为解释器可读取的形式,即转换为字符串格式;
例如:
>>> od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3'})
>>> nod = od.__repr__()
>>> nod
"OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])"
>>> type(nod)
<class 'str'>
'''
def __reduce__(self):
'Return state information for pickling'
inst_dict = vars(self).copy()
for k in vars(OrderedDict()):
inst_dict.pop(k, None)
return self.__class__, (), inst_dict or None, None, iter(self.items())
'''
返回pickling状态的信息;
例如:
od = OrderedDict({'k1':'v1','k2':'v2','k3':'v3'})
>>> nod = od.__reduce__()
>>> nod
(<class 'collections.OrderedDict'>, (), None, None, <odict_iterator object at 0x0000027FF1D11F10>)
>>> type(nod)
<class 'tuple'>
'''
def copy(self):
'od.copy() -> a shallow copy of od'
return self.__class__(self)
'''
浅拷贝;
'''
@classmethod
def fromkeys(cls, iterable, value=None):
'''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.
If not specified, the value defaults to None.
'''
self = cls()
for key in iterable:
self[key] = value
return self
'''
获取S的keys,并生成新字典 如果v参数未指定,则值默认为None;
例如:
>>> od1 = OrderedDict({'k1':'v1','k2':'v2','k3':'v3'})
>>> od2 = OrderedDict({'k4':'v4','k5':'v5','k6':'v6'})
>>> od2.fromkeys(od1,'v')
OrderedDict([('k1', 'v'), ('k2', 'v'), ('k3', 'v')])
>>> od2
OrderedDict([('k4', 'v4'), ('k5', 'v5'), ('k6', 'v6')])
>>> od2.fromkeys(od1)
OrderedDict([('k1', None), ('k2', None), ('k3', None)])
'''
def __eq__(self, other):
'''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive
while comparison to a regular mapping is order-insensitive.
'''
if isinstance(other, OrderedDict):
return dict.__eq__(self, other) and all(map(_eq, self, other))
return dict.__eq__(self, other)
'''
od.__eq__(y) 等同于 od==y
有序字典等同于判断,即判断od是否等于y,返回布尔值;
例如:
>>> od1 = OrderedDict({'k1':'v1','k2':'v2','k3':'v3'})
>>> od2 = OrderedDict({'k4':'v4','k5':'v5','k6':'v6'})
>>> od1.__eq__(od2)
False
>>> od2 = OrderedDict({'k1':'v1','k2':'v2','k3':'v3'})
>>> od1.__eq__(od2)
True
>>> od1 == od2
True
'''
def __ne__(self, other):
'od.__ne__(y) <==> od!=y'
return not self == other
'''
有序字典等不同于判断,即判断od是否不等于y,返回布尔值;
例如:
>>> od1 = OrderedDict({'k1':'v1','k2':'v2','k3':'v3'})
>>> od2 = OrderedDict({'k4':'v4','k5':'v5','k6':'v6'})
>>> od1.__ne__(od2)
True
>>> od1 != od2
True
>>> od2 = OrderedDict({'k1':'v1','k2':'v2','k3':'v3'})
>>> od1.__ne__(od2)
False
'''
# -- the following methods support python 3.x style dictionary views --
def viewkeys(self):
"od.viewkeys() -> a set-like object providing a view on od's keys"
return KeysView(self)
'''
返回一个包含key的类似集合的对象; (Python2特有,Python3已删除)
'''
def viewvalues(self):
"od.viewvalues() -> an object providing a view on od's values"
return ValuesView(self)
'''
返回一个包含value的类似集合的对象; (Python2特有,Python3已删除)
'''
def viewitems(self):
"od.viewitems() -> a set-like object providing a view on od's items"
return ItemsView(self)
'''
返回一个包含所有(key, value)类似集合的对象; (Python2特有,Python3已删除)
'''
3、默认字典(defaultdict)函数说明
defaultdict是对字典的类型的补充,他默认给字典的值设置了一个类型;
class defaultdict(dict):
"""
defaultdict(default_factory[, ...]) --> dict with default factory
The default factory is called without arguments to produce
a new value when a key is not present, in __getitem__ only.
A defaultdict compares equal to a dict with the same items.
All remaining arguments are treated the same as if they were
passed to the dict constructor, including keyword arguments.
"""
'''
当不存在键时,仅在__getitem__调用中,默认字典可以不带参数以生成新值;
默认字典与普通字典基本相同;
所有参数都都与dict字典相同(包括关键字参数),执行时均被传递给dict的构造函数;
'''
def copy(self): # real signature unknown; restored from __doc__
""" D.copy() -> a shallow copy of D. """
pass
'''
浅拷贝;
例如:
>>> from collections import defaultdict
>>> dd1 = defaultdict(list)
>>> dd1['k1']
[]
>>> dd1
defaultdict(<class 'list'>, {'k1': []})
>>> dd2 = dd1.copy()
>>> dd2
defaultdict(<class 'list'>, {'k1': []})
'''
def __copy__(self, *args, **kwargs): # real signature unknown
""" D.copy() -> a shallow copy of D. """
'''
浅拷贝,等同于D.copy();
'''
pass
def __getattribute__(self, name): # real signature unknown; restored from __doc__
""" x.__getattribute__('name') <==> x.name """
pass
def __init__(self, default_factory=None, **kwargs): # known case of _collections.defaultdict.__init__
"""
defaultdict(default_factory[, ...]) --> dict with default factory
The default factory is called without arguments to produce
a new value when a key is not present, in __getitem__ only.
A defaultdict compares equal to a dict with the same items.
All remaining arguments are treated the same as if they were
passed to the dict constructor, including keyword arguments.
# (copied from class doc)
"""
pass
'''
构造方法;
'''
def __missing__(self, key): # real signature unknown; restored from __doc__
"""
__missing__(key) # Called by __getitem__ for missing key; pseudo-code:
if self.default_factory is None: raise KeyError((key,))
self[key] = value = self.default_factory()
return value
"""
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
'''
x.__repr__()等同于repr(x)
转化为解释器可读取的形式,即转换为字符串格式;
'''
default_factory = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
4、可命名元组(namedtuple)函数说明
根据nametuple可以创建一个包含tuple所有功能以及其他功能的类型。
################################################################################
### namedtuple
################################################################################
_class_template = """\
from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict
class {typename}(tuple):
'{typename}({arg_list})'
__slots__ = ()
_fields = {field_names!r}
def __new__(_cls, {arg_list}):
'Create new instance of {typename}({arg_list})'
return _tuple.__new__(_cls, ({arg_list}))
@classmethod
def _make(cls, iterable, new=tuple.__new__, len=len):
'Make a new {typename} object from a sequence or iterable'
result = new(cls, iterable)
if len(result) != {num_fields:d}:
raise TypeError('Expected {num_fields:d} arguments, got %d' % len(result))
return result
def _replace(_self, **kwds):
'Return a new {typename} object replacing specified fields with new values'
result = _self._make(map(kwds.pop, {field_names!r}, _self))
if kwds:
raise ValueError('Got unexpected field names: %r' % list(kwds))
return result
def __repr__(self):
'Return a nicely formatted representation string'
return self.__class__.__name__ + '({repr_fmt})' % self
def _asdict(self):
'Return a new OrderedDict which maps field names to their values.'
return OrderedDict(zip(self._fields, self))
def __getnewargs__(self):
'Return self as a plain tuple. Used by copy and pickle.'
return tuple(self)
{field_defs}
"""
_repr_template = '{name}=%r'
_field_template = '''\
{name} = _property(_itemgetter({index:d}), doc='Alias for field number {index:d}')
'''
def namedtuple(typename, field_names, *, verbose=False, rename=False, module=None):
"""Returns a new subclass of tuple with named fields.
>>> Point = namedtuple('Point', ['x', 'y'])
>>> Point.__doc__ # docstring for the new class
'Point(x, y)'
>>> p = Point(11, y=22) # instantiate with positional args or keywords
>>> p[0] + p[1] # indexable like a plain tuple
33
>>> x, y = p # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y # fields also accessible by name
33
>>> d = p._asdict() # convert to a dictionary
>>> d['x']
11
>>> Point(**d) # convert from a dictionary
Point(x=11, y=22)
>>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
Point(x=100, y=22)
"""
'''
返回具有命名字段的元组的新子类;
'''
# Validate the field names. At the user's option, either generate an error
# message or automatically replace the field name with a valid name.
if isinstance(field_names, str):
field_names = field_names.replace(',', ' ').split()
field_names = list(map(str, field_names))
typename = str(typename)
if rename:
seen = set()
for index, name in enumerate(field_names):
if (not name.isidentifier()
or _iskeyword(name)
or name.startswith('_')
or name in seen):
field_names[index] = '_%d' % index
seen.add(name)
for name in [typename] + field_names:
if type(name) is not str:
raise TypeError('Type names and field names must be strings')
if not name.isidentifier():
raise ValueError('Type names and field names must be valid '
'identifiers: %r' % name)
if _iskeyword(name):
raise ValueError('Type names and field names cannot be a '
'keyword: %r' % name)
seen = set()
for name in field_names:
if name.startswith('_') and not rename:
raise ValueError('Field names cannot start with an underscore: '
'%r' % name)
if name in seen:
raise ValueError('Encountered duplicate field name: %r' % name)
seen.add(name)
# Fill-in the class template
class_definition = _class_template.format(
typename = typename,
field_names = tuple(field_names),
num_fields = len(field_names),
arg_list = repr(tuple(field_names)).replace("'", "")[1:-1],
repr_fmt = ', '.join(_repr_template.format(name=name)
for name in field_names),
field_defs = '\n'.join(_field_template.format(index=index, name=name)
for index, name in enumerate(field_names))
)
# Execute the template string in a temporary namespace and support
# tracing utilities by setting a value for frame.f_globals['__name__']
namespace = dict(__name__='namedtuple_%s' % typename)
exec(class_definition, namespace)
result = namespace[typename]
result._source = class_definition
if verbose:
print(result._source)
# For pickling to work, the __module__ variable needs to be set to the frame
# where the named tuple is created. Bypass this step in environments where
# sys._getframe is not defined (Jython for example) or sys._getframe is not
# defined for arguments greater than 0 (IronPython), or where the user has
# specified a particular module.
if module is None:
try:
module = _sys._getframe(1).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
pass
if module is not None:
result.__module__ = module
return result
相关文章