Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does python reverse the dictionary

Shulou Source: shulou.com Published: 2022-06-01 00:56:27 10月04日 Update

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!

Tags: Dictionary code that is function scenario problem learning same content feature style recommendation programming look familiar elegant handy one line method readability object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Tech Info Shulou Technology Shulou Information macOS