Table of Contents 1. class collections.Counter([iterable-or-mapping]) 1.1. 例子 1.2. 使用实例 2. To Be Continued class collections.Counter([iterable-or-mapping]) Counter 是实现的 dict 的一个子类,可以用来方便地计数。 例子 举个计数的例子,需要统计一个文件中,每个单词出现的次数。实现方法如下 # 普通青年 d = {} with open('/etc/passwd') as f: for line in f: for word in line.strip().split(':'): if word not in d: d[word] = 1 else: d[word] += 1