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

Python dictionary some common magic methods and what are the interview questions encountered

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

Share

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

This article is about some common magic methods and interview questions encountered in python Dictionary. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

I. introduction to the dictionary

The dict type is not only widely used in various programs, it is also the cornerstone of the Python language. The dictionary can be seen in the namespace of the module, the properties of the instance, and the keyword parameters of the function. The built-in functions associated with it are in the _ _ builtins__.__dict__ module. Because dictionaries are so important, Python has highly optimized its implementation, and hash tables are the root cause of the excellent performance of dictionary types.

Compared with the regular dictionary, the OrderDict object maintains a two-way linked list sorted according to the key insertion order, and the newly inserted elements are placed at the end of the linked list, thus realizing the function of remembering the insertion order. However, after the python3.7 version, the built-in dictionary has implemented the same capability, and the reversed () method has been provided in the python3.8 version, so there is no need for OrderDict.

Second, common mapping methods:

1. Check whether k is in dictionary d

D. Shipping containers _ (k)

two。 Let the dictionary d return the value corresponding to the key k in the form of d [k]

D. Recipe getitemology _ (k)

3. This method is called when _ _ getitem__ cannot find the corresponding key

D. Collect missingsong _ (k)

4. Get an attribute value of the dictionary

D. Assign _ (* args, * * kwargs)

5. If there is a key k in the dictionary, set its corresponding value to default, and then return this value; if not, let d [k] = default, and then return default

D.setdefault (k, [default])

Third, face-to-face questions encountered

1. The dictionary accesses the element by attribute access. If you want to get value by attribute access, how to access the element through "." To access dictionary elements in the form

Class mydict (dict):

Def _ getattr__ (self, item): try: return self [item] except: raise AttributeError (item) myd = mydict (axi1, baux2) print (myd.a) print (myd.c)

two。 There is a dictionary, D1 = {"a": (1mague 2p3), "b": (4pc6), "c": (2pyrrine 8)}. The value type is a tuple that sorts dictionaries in reverse order according to the first subscript element of the value value. The result of D1 sorting is [('baked, (4, 6, 6)), (' cased, (2, 5, 8)), ('averse, (1, 2, 3))]

D1 = {"a": (1meme 2p3), "b": (4pje 6pje 6), "c": (2meme 5pas 8)} res = sorted (d1.items (), key=lambda x d1.items x [1] [0], reverse=True) print (res)

3. There is a list that contains multiple dictionaries, such as:

Students = [{"name": "Stanley", "age": 22, "score": 92}, {"name": "Peter", "age": 19, "score": 99}, {"name": "Well", "age": 23, "score": 82}, {"name": "Bob", "age": 20, "score": 88}, {"name": "Lily", "age": 22 "score": 95}]

Sort the above data in reverse order by score and then by age.

From operator import itemgetterstudents_by_score_age = sorted (students, key=itemgetter ("score", "age"), reverse=True) these are some common magic methods and interview questions encountered in the python dictionary. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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