defaultdict & Counter

在使用python原生的数据结构dict的时候,如果d[key]这样的方式访问,当指定的key不存在时,会抛出keyError异常。但是如果使用defaultdict(导入collections),只要你传入一个默认的工厂方法,那么请求一个不存在的key时,便会调用这个工厂方法使用其结果来作为这个key的默认值。
defaultdict在使用的时候需要传一个工厂函数(function_factory),来构建一个类似dict的对象,该对象具有默认值,默认值通过调用工厂函数生成。比如工厂函数用的list,默认值就是[]
举例说明:
统计一个文件中每个单词出现的次数。

常见逻辑写法

d = {}
with open(‘xxx.txt’) as f:
for line iin fin:
    for word in line,strip():
        if word not in d:
            d[word] = 1
        else:
            d[word] += 1

使用defaultdict

from collections import defaultdict
d = defaultdict(int)
with open(‘xxx.txt’) as f:
    for line iin fin:
        for word in line,strip():
            d[word] += 1

使用setdefault, 我们知道defaultdict() 的用法和dict.setdefault(key,[])比较类似。

d = {}
with open(‘xxx.txt’) as fin:
for line iin fin:
    for word in line.strip():
        d[word] = d.setdefault(word,0) + 1 

使用Counter

from collections import Counter
word_counrs = Counter()
with open(‘xxx.txt’) as fin:
for line in fin:
    word_counrs.update(line.strip())

如果输出出现次数最多的前三位,我们或许会这样写:

result = sorted(result.items(), key = lambda d:d[1],reverse = True)[:3]

但是使用Counter就简单了:

result = word_counrs.most_common(3)
原文地址:https://www.cnblogs.com/guoxueyuan/p/6846045.html