How does collections.defaultdict work?

前端 未结 15 2020
离开以前
离开以前 2020-11-22 12:50

I\'ve read the examples in python docs, but still can\'t figure out what this method means. Can somebody help? Here are two examples from the python docs

>         


        
15条回答
  •  长发绾君心
    2020-11-22 12:56

    Well, defaultdict can also raise keyerror in the following case:

        from collections import defaultdict
        d = defaultdict()
        print(d[3]) #raises keyerror
    

    Always remember to give argument to the defaultdict like defaultdict(int).

提交回复
热议问题