Python中Collections模块的Counter容器类使用教程
|
1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict、set、list、tuple以外的一些特殊的容器类型,分别是: OrderedDict类:排序字典,是字典的子类。引入自2.7。 2.Counter类 Counter类的目的是用来跟踪值出现的次数。它是一个无序的容器类型,以字典的键值对形式存储,其中元素作为key,其计数作为value。计数值可以是任意的Interger(包括0和负数)。Counter类和其他语言的bags或multisets很相似。 2.1 创建 下面的代码说明了Counter类创建的四种方法: Counter类的创建Python
>>> c = Counter() # 创建一个空的Counter类
>>> c = Counter('gallahad') # 从一个可iterable对象(list、tuple、dict、字符串等)创建
>>> c = Counter({'a': 4,'b': 2}) # 从一个字典对象创建
>>> c = Counter(a=4,b=2) # 从一组键值对创建
>>> c = Counter() # 创建一个空的Counter类
>>> c = Counter('gallahad') # 从一个可iterable对象(list、tuple、dict、字符串等)创建
>>> c = Counter({'a': 4,b=2) # 从一组键值对创建
2.2 计数值的访问与缺失的键
当所访问的键不存在时,返回0,而不是KeyError;否则返回它的计数。 计数值的访问Python
>>> c = Counter("abcdefgab")
>>> c["a"]
2
>>> c["c"]
1
>>> c["h"]
0
>>> c = Counter("abcdefgab")
>>> c["a"]
2
>>> c["c"]
1
>>> c["h"]
0
2.3 计数器的更新(update和subtract) 可以使用一个iterable对象或者另一个Counter对象来更新键值。 计数器的更新包括增加和减少两种。其中,增加使用update()方法: 计数器的更新(update)Python
>>> c = Counter('which')
>>> c.update('witch') # 使用另一个iterable对象更新
>>> c['h']
3
>>> d = Counter('watch')
>>> c.update(d) # 使用另一个Counter对象更新
>>> c['h']
4
>>> c = Counter('which')
>>> c.update('witch') # 使用另一个iterable对象更新
>>> c['h']
3
>>> d = Counter('watch')
>>> c.update(d) # 使用另一个Counter对象更新
>>> c['h']
4
计数器的更新(subtract)Python
>>> c = Counter('which')
>>> c.subtract('witch') # 使用另一个iterable对象更新
>>> c['h']
1
>>> d = Counter('watch')
>>> c.subtract(d) # 使用另一个Counter对象更新
>>> c['a']
-1
>>> c = Counter('which')
>>> c.subtract('witch') # 使用另一个iterable对象更新
>>> c['h']
1
>>> d = Counter('watch')
>>> c.subtract(d) # 使用另一个Counter对象更新
>>> c['a']
-1
2.4 键的删除 当计数值为0时,并不意味着元素被删除,删除元素应当使用del。 键的删除Python
>>> c = Counter("abcdcba")
>>> c
Counter({'a': 2,'c': 2,'b': 2,'d': 1})
>>> c["b"] = 0
>>> c
Counter({'a': 2,'d': 1,'b': 0})
>>> del c["a"]
>>> c
Counter({'c': 2,'d': 1})
>>> c = Counter("abcdcba")
>>> c
Counter({'a': 2,'d': 1})
返回一个迭代器。元素被重复了多少次,在该迭代器中就包含多少个该元素。所有元素按照字母序排序,个数小于1的元素不被包含。 elements()方法Python >>> c = Counter(a=4,b=2,c=0,d=-2) >>> list(c.elements()) ['a','a','b','b'] >>> c = Counter(a=4,'b'] 2.6 most_common([n]) 返回一个TopN列表。如果n没有被指定,则返回所有元素。当多个元素计数值相同时,按照字母序排列。 most_common()方法Python
>>> c = Counter('abracadabra')
>>> c.most_common()
[('a',5),('r',2),('b',('c',1),('d',1)]
>>> c.most_common(3)
[('a',2)]
>>> c = Counter('abracadabra')
>>> c.most_common()
[('a',2)]
2.7 fromkeys 未实现的类方法。 2.8 浅拷贝copy 浅拷贝copyPython
>>> c = Counter("abcdcba")
>>> c
Counter({'a': 2,'d': 1})
>>> d = c.copy()
>>> d
Counter({'a': 2,'d': 1})
2.9 算术和集合操作 +、-、&、|操作也可以用于Counter。其中&和|操作分别返回两个Counter对象各元素的最小值和最大值。需要注意的是,得到的Counter对象将删除小于1的元素。 Counter对象的算术和集合操作Python
>>> c = Counter(a=3,b=1)
>>> d = Counter(a=1,b=2)
>>> c + d # c[x] + d[x]
Counter({'a': 4,'b': 3})
>>> c - d # subtract(只保留正数计数的元素)
Counter({'a': 2})
>>> c & d # 交集: min(c[x],d[x])
Counter({'a': 1,'b': 1})
>>> c | d # 并集: max(c[x],d[x])
Counter({'a': 3,'b': 2})
>>> c = Counter(a=3,'b': 2})
3.常用操作 下面是一些Counter类的常用操作,来源于Python官方文档 Counter类常用操作Python sum(c.values()) # 所有计数的总数 c.clear() # 重置Counter对象,注意不是删除 list(c) # 将c中的键转为列表 set(c) # 将c中的键转为set dict(c) # 将c中的键值对转为字典 c.items() # 转为(elem,cnt)格式的列表 Counter(dict(list_of_pairs)) # 从(elem,cnt)格式的列表转换为Counter类对象 c.most_common()[:-n:-1] # 取出计数最少的n个元素 c += Counter() # 移除0和负值 sum(c.values()) # 所有计数的总数 c.clear() # 重置Counter对象,注意不是删除 list(c) # 将c中的键转为列表 set(c) # 将c中的键转为set dict(c) # 将c中的键值对转为字典 c.items() # 转为(elem,cnt)格式的列表转换为Counter类对象 c.most_common()[:-n:-1] # 取出计数最少的n个元素 c += Counter() # 移除0和负值 4.实例 def is_anagram(word1,word2): """Checks whether the words are anagrams. word1: string word2: string returns: boolean """ return Counter(word1) == Counter(word2) Counter如果传入的参数是字符串,就会统计字符串中每个字符出现的次数,如果两个字符串由相同的字母集合颠倒顺序而成,则它们Counter的结果应该是一样的。 4.2多元集合(MultiSets)
class Multiset(Counter):
"""A multiset is a set where elements can appear more than once."""
def is_subset(self,other):
"""Checks whether self is a subset of other.
other: Multiset
returns: boolean
"""
for char,count in self.items():
if other[char] < count:
return False
return True
# map the <= operator to is_subset
__le__ = is_subset
4.3概率质量函数
class Pmf(Counter):
"""A Counter with probabilities."""
def normalize(self):
"""Normalizes the PMF so the probabilities add to 1."""
total = float(sum(self.values()))
for key in self:
self[key] /= total
def __add__(self,other):
"""Adds two distributions.
The result is the distribution of sums of values from the
two distributions.
other: Pmf
returns: new Pmf
"""
pmf = Pmf()
for key1,prob1 in self.items():
for key2,prob2 in other.items():
pmf[key1 + key2] += prob1 * prob2
return pmf
def __hash__(self):
"""Returns an integer hash value."""
return id(self)
def __eq__(self,other):
return self is other
def render(self):
"""Returns values and their probabilities,suitable for plotting."""
return zip(*sorted(self.items()))
normalize: 归一化随机变量出现的概率,使它们之和为1
d6 = Pmf([1,2,3,4,5,6])
d6.normalize()
d6.name = 'one die'
print(d6)
Pmf({1: 0.16666666666666666,2: 0.16666666666666666,3: 0.16666666666666666,4: 0.16666666666666666,5: 0.16666666666666666,6: 0.16666666666666666})
使用add,我们可以计算出两个骰子和的分布: d6_twice = d6 + d6 d6_twice.name = 'two dices' for key,prob in d6_twice.items(): print(key,prob) 借助numpy.sum,我们可以直接计算三个骰子和的分布: import numpy as np d6_thrice = np.sum([d6]*3) d6_thrice.name = 'three dices' (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
