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 use the setdefault and defaultdict functions of python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the relevant knowledge of "how to use the setdefault and defaultdict functions of python". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use the setdefault and defaultdict functions of python" can help you solve the problem.

There is a need for a list of key-value pairs such as (key, value), which should be converted into a dictionary object and value with the same key as a group. Look at the code:

Data = [("p", 1), ("p", 2), ("p", 3)

("h", 1), ("h", 2), ("h", 3)]

To convert to

Result = {'packs: [1,2,3],' hints: [1,2,3]}

The following method is what everyone can think of: first determine whether there is key in result, initialize a list for it if not, and append the value directly to the list. But this code is not very elegant in Python.

Result = {}

For (key, value) in data:

If key in result:

Result [key] .append (value)

Else:

Result [key] = [value] setdefault

A more elegant way is to use the setdefault method, which is an instance method of a dictionary object that takes two parameters, and the usage is similar to the dictionary's get method, but more powerful than get. It can set a default value for the key of the dictionary (if the key is not in the dictionary)

Define

Def setdefault (self, k, d=None):

"" D.setdefault (k [, d])-> D.get (k [, d])

Also set D [k] = d if k not in D

"

Value = D.get (k _ r _ d)

If k not in D:

D [k] = d

Return value

Of course, the internal implementation is certainly more efficient than the code above. The difference between the two is that the default value set by the L get method does not change the original dictionary, while the default value set by setdefault changes the original dictionary value.

> d = {"x": 3}

> y = d.get ("y", 4)

> > y

four

> > d

{'xboys: 3}

Contrast

> y = d.setdefault ("y", 4)

> > y

four

> > d

{'Yee: 4,' x: 3}

So, the previous requirements have this more elegant way of writing:

Result = {}

Data = [("p", 1), ("p", 2), ("p", 3)

("h", 1), ("h", 2), ("h", 3)]

For (key, value) in data:

Result.setdefault (key, []) .append (value) defaultdict

Defaultdict is a factory function under the collections module that builds a dictionary object and takes a function (callable) object as a parameter. The type returned by the parameter is the type that key corresponds to value.

> result = defaultdict (list)

> result

Defaultdict (, {})

> result ['a']

[]

If the parameter is list, it builds a dictionary whose default value is list. For example, the value of result ['a'] defaults to the list object.

Therefore, the previous code can be changed to:

From collections import defaultdict

Result = defaultdict (list)

Data = [("p", 1), ("p", 2), ("p", 3)

("h", 1), ("h", 2), ("h", 3)]

For (key, value) in data:

The result [key] .append (value) about "how to use the setdefault and defaultdict functions of python" ends here. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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