I want to write a triple loop and conditional branch in one line in python

Introduction

A memorandum of knowledge that I encountered while implementing Takashi-kun problem. I learned that it is necessary to write multiple for statements in one sentence when using pulp. I still feel the lack of writing power. .. ..

Note

Triple loop

for x in range(4):
    for y in range(3):
        for z in range(2):
            print(x,y,z)
>>0 0 0
>>0 0 1
>>0 1 0
>>0 1 1
>>0 2 0
>>0 2 1 ...

Is

[(x,y,z) for x in range(4) for y in range(3) for z in range(2)]
>>[(0, 0, 0),
>> (0, 0, 1),
>> (0, 1, 0),
>> (0, 1, 1),
>> (0, 2, 0),
>> (0, 2, 1), ...

Triple loop + 1 conditional branch

for x in range(4):
    if x ==3:
        for y in range(3):
            for z in range(2):
                print(x,y,z)
>>3 0 0
>>3 0 1
>>3 1 0
>>3 1 1
>>3 2 0
>>3 2 1

Is

[(x,y,z) for x in range(4) if x == 3 for y in range(3) for z in range(2)]
>>[(3, 0, 0), (3, 0, 1), (3, 1, 0), (3, 1, 1), (3, 2, 0), (3, 2, 1)]

Triple loop + 2 conditional branches

for x in range(4):
    if x ==3:
        for y in range(3):
            for z in range(2):
                if z == 1:
                    print(x,y,z)
>>3 0 1
>>3 1 1
>>3 2 1

Is

[(x,y,z) for x in range(4) if x == 3 for y in range(3) for z in range(2) if z == 1]
>>[(3, 0, 1), (3, 1, 1), (3, 2, 1)]

at the end

The order in which the for statement turns is complicated.

Recommended Posts

I want to write a triple loop and conditional branch in one line in python
I want to write in Python! (2) Let's write a test
I want to create a window in Python
I want to write to a file with Python
I want to write in Python! (1) Code format check
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to randomly sample a file in Python
I want to work with a robot in python.
I want to write in Python! (3) Utilize the mock
I want to get the file name, line number, and function name in Python 3.4
I want to send a message from Python to LINE Bot
I want to make input () a nice complement in python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to create a pipfile and reflect it in docker
I want to print in a comprehension
I want to build a Python environment
I want to convert a table converted to PDF in Python back to CSV
Connect to postgreSQL from Python and use stored procedures in a loop.
I want to color a part of an Excel string in Python
A note I looked up to make a command line tool in Python
I want to make a web application using React and Python flask
I want to do a monkey patch only partially safely in Python
I want to do Dunnett's test in Python
I want to make a game with Python
I wrote a class in Python3 and Java
I want to merge nested dicts in Python
Make a rock-paper-scissors game in one line (python)
I want to display the progress in Python!
I want to create a priority queue that can be updated in Python (2.7)
I want to write an element to a file with numpy and check it.
I want to exe and distribute a program that resizes images Python3 + pyinstaller
Python program is slow! I want to speed up! In such a case ...
[Python] I want to collect specific files scattered in each folder in one place
I want to iterate a Python generator many times
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to handle optimization with python and cplex
Even in JavaScript, I want to see Python `range ()`!
I tried to implement a pseudo pachislot in Python
I made a LINE BOT with Python and Heroku
Write tests in Python to profile and check coverage
[Python] I want to make a nested list a tuple
I want to use the R dataset in python
I want to run a quantum computer with Python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
I want to use a python data source in Re: Dash to get query results
Created a Python library to write complex comprehensions and reduce in an easy-to-read manner
I want to replace the variables in the python template file and mass-produce it in another file.
I can't sleep until I build a server !! (Introduction to Python server made in one day)
Fizzbuzz in Python (in one line)
I tried to implement a one-dimensional cellular automaton in Python
I want to do something like sort uniq in Python
I want to start a jupyter environment with one command
[Python] I want to get a common set between numpy
I want to start a lot of processes from python
[Python] How to write an if statement in one sentence.
A standard way to develop and distribute packages in Python
I tried "How to get a method decorated in Python"
I created a class in Python and tried duck typing
I want to know the features of Python and pip