您问哪个更有效。假设您正在谈论执行速度:如果您的数据很小,那没关系。如果它是大而典型的,“已经存在”的情况将比“不在字典中”的情况发生得更频繁。这一观察结果解释了一些结果。
下面是一些代码,可以与timeit 模块一起使用,以在没有文件读取开销的情况下探索速度。我冒昧地添加了第 5 种方法,它并非没有竞争力,并且可以在至少 1.5.2 [测试] 之后的任何 Python 上运行。
from collections import defaultdict, Counter
def tally0(iterable):
# DOESN'T WORK -- common base case for timing
d = {}
for item in iterable:
d[item] = 1
return d
def tally1(iterable):
d = {}
for item in iterable:
if item in d:
d[item] += 1
else:
d[item] = 1
return d
def tally2(iterable):
d = {}
for item in iterable:
try:
d[item] += 1
except KeyError:
d[item] = 1
return d
def tally3(iterable):
d = defaultdict(int)
for item in iterable:
d[item] += 1
def tally4(iterable):
d = Counter()
for item in iterable:
d[item] += 1
def tally5(iterable):
d = {}
dg = d.get
for item in iterable:
d[item] = dg(item, 0) + 1
return d
典型运行(在 Windows XP“命令提示符”窗口中):
prompt>\python27\python -mtimeit -s"t=1000*'now is the winter of our discontent made glorious summer by this son of york';import tally_bench as tb" "tb.tally1(t)"
10 loops, best of 3: 29.5 msec per loop
以下是结果(每循环毫秒):
0 base case 13.6
1 if k in d 29.5
2 try/except 26.1
3 defaultdict 23.4
4 Counter 79.4
5 d.get(k, 0) 29.2
另一个计时试验:
prompt>\python27\python -mtimeit -s"from collections import defaultdict;d=defaultdict(int)" "d[1]+=1"
1000000 loops, best of 3: 0.309 usec per loop
prompt>\python27\python -mtimeit -s"from collections import Counter;d=Counter()" "d[1]+=1"
1000000 loops, best of 3: 1.02 usec per loop
Counter 的速度可能是由于它部分用 Python 代码实现,而 defaultdict 完全用 C 语言实现(至少在 2.7 中)。
请注意,Counter() 不仅仅是defaultdict(int) 的“语法糖”——它实现了一个完整的bag 又名multiset 对象——有关详细信息,请参阅文档;如果您需要一些花哨的后期处理,它们可能会让您免于重新发明轮子。如果您只想计算事物,请使用defaultdict。
更新以回应@Steven Rumbalski 的问题:“””我很好奇,如果将可迭代对象移入 Counter 构造函数会发生什么情况:d = Counter(iterable)?(我有python 2.6,无法测试。) """
tally6:d = Count(iterable); return d,需要 60.0 毫秒
您可以查看源代码(SVN 存储库中的collections.py)...这是我的Python27\Lib\collections.py 在iterable 不是映射实例时所做的:
self_get = self.get
for elem in iterable:
self[elem] = self_get(elem, 0) + 1
以前在任何地方见过该代码吗?只是为了调用可在 Python 1.5.2 中运行的代码:-O