[Python] Let's write briefly about comprehensions

Reason for publication

・ Because Qiita's comment mentioned the intensional expression, and I thought it was an opportunity to understand it now. (I've heard the comprehension itself in the "Python Tutorial", but it seemed difficult and I ran away. Now is my chance!)

Comprehension grammar

The grammar itself doesn't seem to be as difficult as it looks. (I ran away because I thought it would be difficult ... sweat)

[Processing content for x in list etc.]

Personally, when I disassembled it as follows, I personally felt comfortable.

test.png

After "for", the form is the same as a normal Python for statement. Impression that the processing content is like a for statement that came before.

Sample code (included notation)

Code that triples each element

naiho.py


num_list = [-4, -3, -2, -1, 0, 1, 2, 3, 4]

naiho = [x * 3 for x in num_list]
print(naiho)

Or a code to put the last name in the name

kawaii.py


gotobun = ["ithika", "nino", "miku", "yotsuba", "itsuki"]

hanayome = ["nakano " + z for z in gotobun]
print(hanayome)

Output result (included notation)

Calculation can be done normally

[-12, -9, -6, -3, 0, 3, 6, 9, 12]

It's easy to create a string. It seems that there are many situations that can be used if devised.

['nakano ithika', 'nakano nino', 'nakano miku', 'nakano yotsuba', 'nakano itsuki']

Sample code (no inclusion notation ver)

If you try to process the above two sample codes with a for statement, it will look like the following.

no_naiho.py


num_list = [-4, -3, -2, -1, 0, 1, 2, 3, 4]
not_naiho = []

for y in num_list:
  y *= 3
  not_naiho.append(y)

print(not_naiho) 

no_kawaii.py


gotobun = ["ithika", "nino", "miku", "yotsuba", "itsuki"]
hanayome = []

for a in gotobun:
  a = "nakano " + a
  hanayome.append(a)

print(hanayome) 

Output result (no inclusion notation ver)

It doesn't change depending on whether it is included or not, but I will write it for the time being.

[-12, -9, -6, -3, 0, 3, 6, 9, 12]
['nakano ithika', 'nakano nino', 'nakano miku', 'nakano yotsuba', 'nakano itsuki']

Impressions

・ I felt that by using the comprehension notation, I could write the for statement more simply than I expected. ・ If you are not familiar with the comprehension notation, write the for and below first, and then write the processing content at the end. I thought I would be less confused.

Recommended Posts

[Python] Let's write briefly about comprehensions
Let's write python with cinema4d.
About python slices
About python comprehension
About Python tqdm.
About python yield
About python, class
About python inheritance
About python, range ()
About python decorators
About python reference
About Python decorators
[Python] About multi-process
Let's write FizzBuzz with an error: Python Version
Let's write a Python program and run it
About Python for loops
Summary about Python scraping
About function arguments (python)
Sort in Python. Next, let's think about the algorithm.
Write about building a Python environment for writing Qiita Qiita
I want to write in Python! (2) Let's write a test
Let's write python code that parses go code and generates go code
[Python] Memo about functions
Summary about Python3 + OpenCV3
About Python, for ~ (range)
Write Python in MySQL
About Python3 character code
[Python] Memo about errors
About Python development environment
Indent comprehensions in Python
Python: About function arguments
Python, about exception handling
About Python Pyramid traversal
About Python3 ... (Ellipsis object)
[Python] Chapter 01-01 About Python (First Python)
[Python] About standard input
About __all__ in python
Let's write Python, Ruby, PHP, Java, JavaScript side respectively
[Python] Chapter 01-03 About Python (Write and execute a program using PyCharm)
[Python] Find out about pip
Let's use def in python
Let's use python janome easily
Write Pandoc filters in Python
About Fabric's support for Python 3
About python objects and classes
Write beta distribution in Python
About Python variables and objects
Write python in Rstudio (reticulate)
About the Python module venv
Think about architecture in python
About python beginner's memorandum function
About the ease of Python
About the enumerate function (python)
About various encodings of Python 3
About Python, len () and randint ()
About Perl, Python, PHP, Ruby
About Python datetime and timezone
A memorandum about correlation [Python]
Write to csv with Python
A memorandum about Python mock
About Python string comparison operators