This is especially useful for nested dicts; rather than testing for existence or calling setdefault() at every level of the dict; the following code below will run works and illustrates an example:
bidict = collections.defaultdict(dict)
bidict['a']['b'] = 1
Both levels of the dict will be initialized correctly automatically.
A more pedestrian usage might be to initialize every value to zero, which can be accomplished simply, since calling int() will always return 0:
termfreq = collections.defaultdict(int)
termfreq['unknownterm'] == 0
No comments:
Post a Comment