Reproduced Python 3 collections.defaultdict () and dict usage and differences

Source: Internet
Author: User

Article Turn from:

Http://www.cnblogs.com/herbert/archive/2013/01/09/2852843.html

Python 3 collections.defaultdict () and dict usage and differences

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 its own assignment, but the values type, which is a class instance of Function_factory , and has a default value. For example, default (int) creates a similar dictionary object in which any values are instances of int , and even a nonexistent Key, D[key] also 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:

Import Collectionss = [(' Yellow ', 1), (' Blue ', 2), (' Yellow ', 3), (' Blue ', 4), (' Red ', 1)]d = collections.defaultdict (list ) for K, V in S:    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 theDict 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 AR Guments.

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 isn't None, it is 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 def Ault_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:

Import Collectionss = [(' Yellow ', 1), (' Blue ', 2), (' Yellow ', 3), (' Blue ', 4), (' Red ', 1)]# DEFAULTDICTD = Collections.def Aultdict (list) for K, V in S:    d[k].append (v) # with Dict and setdefault    g = {}for K, V in S:    G.setdefault (k, []). Append (v)    # Use dicte = {}for K, V in S:    e[k] = v# #list (D.items ()) # #list (G.items ()) # #list (E.items ())

Look at the results

List (D.items ()) [(' Blue ', [2, 4]), (' Red ', [1]), (' Yellow ', [1, 3])]>>> list (G.items ()) [(' Blue ', [2, 4]), (' Red ', [1]), (' Yellow ', [1, 3])]>>> list (E.items ()) [(' Blue ', 4), (' Red ', 1), (' Yellow ', 3)]>>> Ddefaultdict (& Lt;class ' list ', {' Blue ': [2, 4], ' Red ': [1], ' yellow ': [1, 3]} ' >>> g{' Blue ': [2, 4], ' Red ': [1], ' yellow ': [ 1, 3]}>>> e{' Blue ': 4, ' Red ': 1, ' yellow ': 3}>>> d.items () dict_items ([' Blue ', [2, 4]), (' Red ', [1]), (' Yellow ', [1, 3])] >>> d["Blue"][2, 4]>>> D.keys () dict_keys ([' Blue ', ' red ', ' yellow ']) >>> D.default_factory<class ' list ' >>>> d.values () dict_values ([[[2, 4], [1], [1, 3]])

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 () operation 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.

>>> s = ' Mississippi ' >>> d = defaultdict (int) >>> for k in s: ...     D[k] + = 1...>>> list (D.items ()) [(' I ', 4), (' P ', 2), (' s ', 4), (' m ', 1)]

Reproduced Python 3 collections.defaultdict () and dict usage and differences

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.