Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to recognize defaultdict in Python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to understand the defaultdict in Python, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Today our protagonist is defaultdict, and we will also introduce the imitation method _ _ missing__ ().

Default values can be very convenient.

It is well known that in Python, if you access a key that does not exist in the dictionary, a KeyError exception is thrown. Sometimes, however, it is convenient to have a default value for every key in the dictionary. For example, the following example:

Strings = ('puppy',' kitten', 'puppy',' puppy', 'weasel',' puppy', 'kitten',' puppy') counts = {} for kw in strings: counts [kw] + = 1

This example counts the number of times a word appears in strings and records it in the counts dictionary. Each time a word appears, the number of values stored in the key corresponding to counts is increased by 1. But in fact, running this code throws a KeyError exception when each word is counted for the first time, because there is no default value in Python's dict, which can be verified on the Python command line:

> counts = dict () > > counts {} > counts ['puppy'] + = 1 Traceback (most recent call last): File ", line 1, in KeyError:' puppy'

Check using a judgment statement

In that case, the first method you might think of is to store the default value of 1 in counts when the words are counted for the first time. This requires adding a judgment statement when processing:

Strings = ('puppy',' kitten', 'puppy',' puppy', 'weasel',' puppy', 'kitten',' puppy') counts = {} for kw in strings: if kw not in counts: counts [kw] = 1 else: counts [kw] + = 1 # counts: # {'puppy': 5,' weasel': 1, 'kitten': 2}

Use the dict.setdefault () method

You can also set the default value through the dict.setdefault () method:

Strings = ('puppy',' kitten', 'puppy',' puppy', 'weasel',' puppy', 'kitten',' puppy') counts = {} for kw in strings: counts.setdefault (kw, 0) counts [kw] + = 1

The dict.setdefault () method takes two parameters, the first of which is the name of the key, and the second is the default value. If the given key does not exist in the dictionary, the default value provided in the parameter is returned; otherwise, the value saved in the dictionary is returned. Using the return value of the dict.setdefault () method, you can rewrite the code in the for loop to make it more concise:

Strings = ('puppy',' kitten', 'puppy',' puppy', 'weasel',' puppy', 'kitten',' puppy') counts = {} for kw in strings: counts [kw] = counts.setdefault (kw, 0) + 1 uses the collections.defaultdict class

Although the above methods solve the problem that there are no default values in dict to some extent, we will wonder if there is a dictionary that provides default values. The answer is yes, that is collections.defaultdict.

The defaultdict class is like a dict, but it is initialized with a type:

> from collections import defaultdict > dd = defaultdict (list) > dddefaultdict (, {})

The initialization function of the defaultdict class takes a type as an argument, and when the accessed key does not exist, you can instantiate a value as the default value:

> dd ['foo'] [] > > dddefaultdict (, {' foo': []}) > dd ['bar'] .append (' quux') > dddefaultdict (, {'foo': [],' bar': ['quux']})

It is important to note that the default value in this form is valid only when accessed through dict [key] or dict.__getitem__ (key), for reasons described below.

> from collections import defaultdict > dd = defaultdict (list) > something' in dd False > dd.pop ('something') Traceback (most recent call last): File ", line 1, in KeyError:' pop (): dictionary is empty' > dd.get ('something') > dd [' something'] []

In addition to accepting the type name as an argument to the initialization function, the defaultdict class can also use any callable function without parameters, and the return result of the function will then be used as the default value, which makes the default value more flexible. Let's use an example to illustrate how to use the custom function zero () with no arguments as the parameter of the initialization function of the defaultdict class:

> from collections import defaultdict > def zero ():... Return 0... > > dd = defaultdict (zero) > dddefaultdict (, {}) > dd ['foo'] 0 > dddefaultdict (, {' foo': 0})

Use collections.defaultdict to solve the initial word statistics problem, the code is as follows:

From collections import defaultdictstrings = ('puppy',' kitten', 'puppy',' puppy', 'weasel',' puppy', 'kitten',' puppy') counts = defaultdict (lambda: 0) # use lambda to define a simple function for s in strings: counts [s] + = 1

How the defaultdict class is implemented

From the above, you must have known the use of the defaultdict class, so how to implement the default value function in the defaultdict class? The key is to use the method of looking at _ _ missing__ ():

> from collections import defaultdict > print defaultdict.__missing__.__doc____missing__ (key) # Called by _ _ getitem__ for missing key; pseudo-code: if self.default_factory is None: raise KeyError (key) self [key] = value = self.default_factory () return value

By looking at the docstring of the _ _ missing__ () method, you can see that when you use the _ _ getitem__ () method to access a non-existent key, dict [key] is actually a simplified form of the _ _ getitem__ () method. The _ _ missing__ () method is called to get the default value and add the key to the dictionary.

A detailed description of the _ _ missing__ () method can be found in the "Mapping Types-dict" section of the official Python documentation.

According to the documentation, starting with version 2.5, if a subclass derived from dict defines the _ _ missing__ () method, when accessing a key that does not exist, dict [key] calls the _ _ missing__ () method to get the default value.

You can see that although dict supports the _ _ missing__ () method, it does not exist in dict itself and needs to be implemented on its own in a derived subclass. You can simply verify this:

> print dict.__missing__.__doc__Traceback (most recent call last): File "", line 1, in AttributeError: type object 'dict' has no attribute' _ _ missing__'

At the same time, we can do further experiments to define a subclass Missing and implement the _ _ missing__ () method:

Class Missing (dict):... Def _ _ missing__ (self, key):... Return 'missing'. > d = Missing () > > d {} > d [' foo'] 'missing' > d {}

The returned result reflects that the _ _ missing__ () method does work. On this basis, we modify the _ _ missing__ () method slightly so that this subclass, like the defautldict class, sets a default value for keys that do not exist:

Class Defaulting (dict):... Def _ _ missing__ (self, key):... Self [key] = 'default'... Return 'default'. > d = Defaulting () > d {} > d [' foo'] 'default' > d {' foo': 'default'}

Implement the function of class defaultdict in the old version of Python

The defaultdict class has been added since version 2.5 and is not supported in some older versions, so it is necessary to implement a compatible defaultdict class for the older version. This is actually very simple, although the performance may not be as good as the defautldict class that comes with version 2.5, but the functionality is the same.

First, the _ _ getitem__ () method needs to call the _ _ missing__ () method when the access key fails:

Class defaultdict (dict): def _ getitem__ (self, key): try: return dict.__getitem__ (self, key) except KeyError: return self.__missing__ (key)

Second, you need to implement the _ _ missing__ () method to set the default value:

Class defaultdict (dict): def _ getitem__ (self, key): try: return dict.__getitem__ (self, key) except KeyError: return self.__missing__ (key) def _ missing__ (self, key): self [key] = value = self.default_factory () return value

Then, the initialization function of the defaultdict class _ _ init__ () needs to accept the type or callable function parameters:

Class defaultdict (dict): def _ init__ (self, default_factory=None, * a, * * kw): dict.__init__ (self, * a, * * kw) self.default_factory = default_factory def _ getitem__ (self, key): try: return dict.__getitem__ (self Key) except KeyError: return self.__missing__ (key) def _ _ missing__ (self, key): self [key] = value = self.default_factory () return value

Finally, synthesize the above, and complete the code that is compatible with the new and old Python versions in the following ways:

Try: from collections import defaultdict except ImportError: class defaultdict (dict): def _ init__ (self, default_factory=None, * a, * * kw): dict.__init__ (self, * a, * * kw) self.default_factory = default_factory def _ getitem__ (self, key): try: return dict.__getitem__ (self Key) except KeyError: return self.__missing__ (key) def _ _ missing__ (self, key): self [key] = value = self.default_factory () return value Have you mastered how to recognize defaultdict in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report