我注意到经常建议使用具有多个线程的队列,而不是列表和.pop()
.这是因为列表不是线程安全的,还是出于其他原因?
与此问题类似,我想创建一个collections.defaultdict
默认值为零来计算元组列表的元素:
tuples = [('foo', 5), ('foo', 5), ('bar', -5), ('bar', -2)]
totals = defaultdict(0)
for t in tuples:
totals[t[0]] += t[1]
Run Code Online (Sandbox Code Playgroud)
但是defaultdict(0)
说:
TypeError: first argument must be callable
Run Code Online (Sandbox Code Playgroud)
我怎么能创造一个"可赎回"的零?