Python -I tried to restore the dictionary comprehensive notation to its original form-

Currently, I am studying one dictionary comprehensive notation of comprehension. Along the way, I tried to restore the dictionary comprehensive notation to the original long code and it succeeded, so I decided to leave it in the article.

What is a comprehension?

In the first place, comprehensions allow you to compactly create Python data structures from one or more iterators. (Quote: Bill Lubanovic, translated by Yasuki Saito, translated by Takahiro Nagao, "Introduction to Python3", p.104 Publisher: O'Reilly Japan ISBN 978-4-87311-738-6)

Dictionary comprehension is one of the comprehensions, which is a dictionary format that uses keys and values.

Try to write a dictionary comprehensive notation

First, I will write a comprehensive dictionary notation. Each character of the character string "python" is used as the key, and the index of each character is used as the value.

Dictionary comprehensive notation


word = "python"
letter_index = {letter:word.index(letter) for letter in word}
print(letter_count)

>> {"p": 0, "y": 1, "t": 2, "h": 3, "o": 4, "n": 5}

By using the inclusive notation such as the dictionary comprehensive notation in this way, you can write a dictionary of the character string of "python" in one line by turning the for loop. I think it will take some time to get used to it, but I'm sure it will be a lot easier to write code! (Perhaps)

Try to restore the dictionary comprehensive notation to its original form

Now, after practicing Python, I will return the dictionary comprehensive notation to its original form.

Return to the original shape


word = "python"
letter_index = {}
for letter in word:
    letter_index[letter] = word.index(letter)

print(letter_count)

>> {"p": 0, "y": 1, "t": 2, "h": 3, "o": 4, "n": 5}

After all, when I made an empty dictionary and made a dictionary with letter_count using the for loop properly, the code became long. You can see that the dictionary comprehensive notation is useful.

But when I put it back in its original form, it became a Python practice and I'm glad I found a new practice method.

Quote

By Bill Lubanovic, Translated by Yasuki Saito, Translated by Takahiro Nagao, "Introduction to Python3" Publisher: O'Reilly Japan ISBN 978-4-87311-738-6

Recommended Posts

Python -I tried to restore the dictionary comprehensive notation to its original form-
[Python] I tried to summarize the array, dictionary generation method, loop method, list comprehension notation
I tried to summarize the basic form of GPLVM
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
[Python] I tried to graph the top 10 eyeshadow rankings
I tried to solve the problem with Python Vol.1
I tried to summarize the string operations of Python
I tried to find the entropy of the image with python
I tried to simulate how the infection spreads with Python
[Python] I tried to visualize the follow relationship of Twitter
I tried to implement the mail sending function in Python
I tried to enumerate the differences between java and python
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to divide the file into folders with Python
I tried to touch Python (installation)
I tried to move the ball
I tried to estimate the interval.
I tried to solve the ant book beginner's edition with python
I tried to display the video playback time (OpenCV: Python version)
I tried to improve the efficiency of daily work with Python
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to recognize the wake word
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I tried to summarize the graphical modeling.
I tried to implement ADALINE in Python
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
I tried to implement PPO in Python
Python: I tried the traveling salesman problem
[Python] I tried to calculate TF-IDF steadily
I tried to touch Python (basic syntax)
I tried the Python Tornado Testing Framework
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
[Python] I tried to analyze the pitcher who achieved no hit no run
I tried to get the authentication code of Qiita API with Python.
(Python) I tried to analyze 1 million hands ~ I tried to estimate the number of AA ~
I tried to restore because the Oracle database initialization parameter setting failed.
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
I tried to verify and analyze the acceleration of Python by Cython
I tried to streamline the standard role of new employees with Python
I tried to get the movie information of TMDb API with Python
I tried to analyze the New Year's card by myself using python
I tried "smoothing" the image with Python + OpenCV
[Python] I tried substituting the function name for the function name
vprof --I tried using the profiler for Python
[Python] I tried to judge the member image of the idol group using Keras
I tried "differentiating" the image with Python + OpenCV
I tried to optimize while drying the laundry
I tried to save the data with discord
I tried simulating the "birthday paradox" in Python
I tried using the Python library "pykakasi" that can convert kanji to romaji.
I tried the least squares method in Python
I tried to touch the API of ebay
I tried to get CloudWatch data with Python
I tried python programming for the first time.
I tried to correct the keystone of the image