Home > other >  Python defaultdict
Python defaultdict

Time:10-13

In the python defaultdict & lt; The type 'list & gt; , [] this is what mean?

CodePudding user response:

List type,!!!!!!!!!!!!!!!!!!!

CodePudding user response:

The original poster is a wrong number? Or is not a defaultdict definition? I tried it on:
 
> The from the collections import defaultdict
> A=defaultdict (list)
> A
Defaultdict (& lt; The class 'list' & gt; , {})


Here & lt; The class 'list' & gt; Specify a value of each element type will be a list at the back of the inside the {} indicates that a temporary also what all have no,

If we add an element to a:
 
> A [' list_one]. Append (1)
> A
Defaultdict (& lt; The class 'list' & gt; , {' list_one: [1]})


Can be seen in a more than a key-value to (yes, defaultdict essentially is a dict), the key is a content for 'list_one string, the corresponding value of [1] the list, (mentioned above, the value of each element in a is a list,)

This example also demonstrates why defaultdict is called default, in the case, we in the case of 'list_one' does not exist, to add content to it, if you use the Python built-in dict,
 
B={}
B) [r]. 'list_one append (1)

Will go wrong (KeyError), without defaultdict means that, by default, the default, the default value is a list ()),
  • Related