Defaultdict with values defaulted to negative infinity
问题 I want to create a default dictionary in which the default values are negative infinity. I tried doing defaultdict(float("-inf")) but it is not working. How do I do this? 回答1: As the traceback specifically tells you : >>> from collections import defaultdict >>> dct = defaultdict(float('-inf')) Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> dct = defaultdict(float('-inf')) TypeError: first argument must be callable and per the documentation (emphasis mine): If