The use and difference of Python collections.defaultdict () and Dict

Source: Internet
Author: User

In Python there is a module collections, which is interpreted as a data type container module. There is a collections.defaultdict () which is often used. The main thing to say about this.

Review:

Here Defaultdict (function_factory) constructs an object similar to dictionary, where the value of keys is determined by itself, but the type of values is the class instance of Function_factory, and has a default value. For example, default (int) creates a similar dictionary object in which any of the values is an instance of int, and even if it is a nonexistent key, D[key] has a default value, which is the default value of int () 0.

Defaultdict

Dict subclass that calls a factory function to supply missing values.

This is a short explanation.

Defaultdict belongs to a subclass of the built-in function dict, and calls the factory function to provide the missing value.

Compare Halo, what is the factory function:

interpretation from the core programming of Python

Python 2.2 unifies the types and classes, and all of the built-in types are now classes, on top of which the original

The so-called built-in conversion functions like int (), type (), list (), and so on, are now factory functions. Which means that although he

They look a bit like functions, essentially they are classes. When you invoke them, you actually generate a real

Example, like a factory producing goods.

The following familiar factory functions are called built-in functions in the old Python version:

Int (), long (), float (), complex ()

STR (), Unicode (), basestring ()

List (), tuple ()

Type ()

There were no other types of factory functions, and now there are factory functions. In addition, the classes that support new styles

New data types, and the corresponding factory functions are also added. These factory functions are listed below:

Dict ()

BOOL ()

Set (), Frozenset ()

Object ()

Classmethod ()

Staticmethod ()

Super ()

Property ()

File ()

And then look at the use of it:

123456 importcollections=[(‘yellow‘1), (‘blue‘2), (‘yellow‘3), (‘blue‘4), (‘red‘1)]=collections.defaultdict(list)fork, v ins:    d[k].append(v)list(d.items())

Here it starts to get a little bit clear, originally Defaultdict can accept a list of built-in functions as parameters. In fact, List () itself is a built-in function, but after the update, everything in Python is an object, so the list is adapted to a class, the introduction of a list of the time to produce an instance of a class.

Still don't understand, see Defaultdict's help explanation

Class Collections.defaultdict ([default_factory[, ...])

Returns a new Dictionary-like object. Defaultdict is a subclass of the built-in Dict class. It overrides one method and adds one writable instance variable. The remaining functionality are the same as for the Dict class and are not documented here.

First of all, Collections.defaultdict will return an object similar to dictionary, note that it is a similar object, not exactly the same object. This defaultdict and Dict class are almost the same, except that it overloads a method and adds a writable instance variable. (A writable instance variable, I still don't understand)

The first argument provides the initial value for the Default_factory attribute; It defaults to None. All remaining arguments is treated the same as if they were passed to the Dict constructor, including keyword arguments.

Defaultdict objects support the following method in addition to the standard DICT operations:

__MISSING__ (Key)

If the Default_factory attribute is None, this raises a Keyerror exception with the key as argument.

If Default_factory is not None, it's called without arguments to provide a default value for the given key, this value is Inserted in the dictionary for the key, and returned.

The main concern is that if default_factory is not none, the default_factory will be called in a parameterless form, providing a default value to the ___missing__ method key. This default value is inserted as a key into the data dictionary and then returned.

Very faint. There is a __missing__ method, the __missing__ method is the built-in method of Collections.defaultdict ().

If calling Default_factory raises an exception this exception is propagated unchanged.

This method was called by the __getitem__ () method of the Dict class when the requested key was not found; Whatever it returns or raises is then returned or raised by __getitem__ ().

Note that __missing__ () was not called for any operations besides __getitem__ (). This means that get () would, like normal dictionaries, return None as a default rather than using Default_factory.

Defaultdict objects support the following instance variable:

Default_factory

This attribute was used by the __missing__ () method; It is initialized from the first argument to the constructor, if present, or to None, if absent.

It seems that this document is difficult to read. Look directly at the example:

123456789101112131415161718 importcollections=[(‘yellow‘1), (‘blue‘2), (‘yellow‘3), (‘blue‘4), (‘red‘1)]# defaultdict=collections.defaultdict(list)fork, v ins:    d[k].append(v)# Use dict and setdefault   = {}fork, v ins:    g.setdefault(k, []).append(v)     # Use dict={}fork, v in s:    e[k] =v##list(d.items())##list(g.items())##list(e.items())

Look at the results

12345678910111213141516171819202122 list(d.items())[(‘blue‘, [24]), (‘red‘, [1]), (‘yellow‘, [13])]>>> list(g.items())[(‘blue‘, [24]), (‘red‘, [1]), (‘yellow‘, [13])]>>> list(e.items())[(‘blue‘4), (‘red‘1), (‘yellow‘3)]>>> ddefaultdict(<class‘list‘>, {‘blue‘: [24], ‘red‘: [1], ‘yellow‘: [13]})>>> g{‘blue‘: [24], ‘red‘: [1], ‘yellow‘: [13]}>>> e{‘blue‘4‘red‘1‘yellow‘3}>>> d.items()dict_items([(‘blue‘, [24]), (‘red‘, [1]), (‘yellow‘, [13])])>>> d["blue"][24]>>> d.keys()dict_keys([‘blue‘‘red‘‘yellow‘])>>> d.default_factory<class‘list‘>>>> d.values()dict_values([[24], [1], [13]])

Can see

Collections.defaultdict (list) uses the same effect as using Dict.setdefault ().

That's what Python help says.

When each key was encountered for the first time, it was not already in the mapping; So a entry is automatically created using the Default_factory function which returns an empty list. The List.append () operation then attaches the value to the new list. When Keys is encountered again, the look-up proceeds normally (returning the list for that key) and the List.append () ope Ration adds another value to the list. This technique are simpler and faster than an equivalent technique using Dict.setdefault ():

This method is equivalent to Dict.setdefault (), but faster.

It is necessary to see Dict.setdefault ()

SetDefault (key[, default])

If key is in the dictionary, return its value. If not, insert key with a value of default and return default. Default defaults to None.

If the key is already stored in the dictionary, return value. If key does not exist, insert key and a default value to return to default. The default defaults is None.

However, it is important to note that defaultdict is equivalent to Dict.setdefault, which is different from the direct assignment below. As you can see from the results, the direct assignment is overwritten.

From the last d.values and d["Blue"], the use of the latter is actually the same as the usage of dict, the only difference is the problem of initialization. Defaultdict can use the factory function to bring a default value to the initial keyi.

This default value may be empty list[] defaultdict (list), perhaps 0, defaultdict (int).

Take a look at the following example.

defaultdict (int) The d here actually generates a data dictionary with key that defaults to 0. You can imagine it as d[key] = int default (the value of the Int factory function is 0)

D[k] So can be read directly d["M"] + = 1 is d["M"] is the default value 0+1 = 1

The reason behind it is the same.

1234567 >>> s =‘mississippi‘>>> d =defaultdict(int)>>> forins:...     d[k] +=1...>>> list(d.items())[(‘i‘4), (‘p‘2), (‘s‘4), (‘m‘1)]

The use and difference of Python collections.defaultdict () and Dict

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.