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 does python reverse the dictionary

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains the "python how to reverse the dictionary", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "python how to reverse the dictionary" bar!

In some specific scenarios, you will encounter the need to interchange the key and value of the dictionary, for example:

D = {'astatine: 1,' breadflower 2}

Become

D = {1: 'asides, 2:' b'}

The first thing you can think of is to traverse the dictionary and exchange key-value for value-key one by one and put it in the new dictionary.

D = {'averse: 1,' baked: 2}

New_d = dict ()

For key in d:

Value = d [key]

New_ d [value] = key

In fact, if you know the concept of deduction, you can do it with a single line of code. Since Python2.7 and Python3 added list derivation, collection derivation, and dictionary derivation, it has been easy to solve this kind of problem. The dictionary derivation can be used to write gracefully:

New_d = {v: k for k, v in d.items ()}

If you are familiar with list derivation, the above code looks familiar to you, which is to interchange key,value to form a new dictionary. It may also be recommended that you use functional programming-style code to implement:

> d = {'averse: 1,' bounded rig 2}}

> dict ((map (reversed, d.items ()

{1: 'asides, 2:' b'}

Functional programming code doesn't look that readable, and it's not as efficient as deductive code. We can prove it with timeit.

> import timeit

> timeit.timeit ("dict ((map (reversed, {'averse: 1,' baked: 2}. Items ())", number=100000)

0.16313515009824187

> timeit.timeit ("{v: k for k, v in {'averse: 1,' baked: 2} .items ()}", number=100000)

0.06065327790565789

At first glance, the speed is nearly 3 times slower, and functions such as map, filter, and reduce have rarely been recommended since Python introduced deductive features.

Let's think about a question: why doesn't the dictionary itself provide such an API interface for us to use?

According to the characteristics of the dictionary:

The key of the dictionary must be unique, the same key will be overwritten, and the value can be the same, so this reversal, that is, key and value are both unique scenarios.

Key must be hash, so if value is an object that is not hash, such as a list, it cannot be used as a key.

Therefore, dictionary inversion is only suitable for certain scenarios and is not suitable for use as a general operation, so it is not provided in the dictionary API.

Thank you for your reading, the above is the content of "how to reverse the dictionary of python", after the study of this article, I believe you have a deeper understanding of the problem of how to reverse the dictionary of python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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