[Python] A program that creates a two-dimensional array by combining integers

[Python] A program that creates a multidimensional array by combining integers

This is a memo for myself.

▼ Question

--Create a unique two-dimensional array of [i, j, k]. --The condition of i, j, k is determined by the given positive integers x, y, z

URL

▼sample input

python


x,y,z,n=1,1,1,2

▼sample output

python


[[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]

▼my answer

python


if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())

    res=[]

for i in range(x+1):
    for j in range(y+1):
        for k in range(z+1):
            if i+j+k != n:
                res.append([i,j,k])

print(res)

For [i, j, k], turn the for statement one by one. (Turn a total of 3 times)
## Simplified with inclusion notation `Processing for Variables in Iterable if Condition` └ ** Process only if the if condition is met.

python


if __name__ == '__main__':
    x,y,z,n = [int(input()) for _ in range(4)]
    
    print([[i,j,k] for k in range(z+1) for j in range(y+1) for i in range(x+1)if i+j+k != n])   

** ▼ Difference from ordinary for statement ** The execution result becomes list type. └ No need to add or add elements such as append

・ ** for _ in range (n) ** Turn the process n times. Since variables are unnecessary, insert "_" for convenience. └ Blank is an error

Blank for


i=-1
for  in range(4):
    i+=1
    print(i)

#output
SyntaxError: invalid syntax

Recommended Posts

[Python] A program that creates a two-dimensional array by combining integers
[Python] A program that creates stairs with #
A program that plays rock-paper-scissors using Python
[Python] A program that rounds the score
[Python] A program that finds a pair that can be divided by a specified value
A set of integers that satisfies ax + by = 1.
[Python / Tkinter] A class that creates a scrollable Frame
A program that removes duplicate statements in Python
A program that sends a fixed amount of mail at a specified time by Python
[Python] A program that counts the number of valleys
[Python] A program that compares the positions of kangaroos.
A Python program that converts ical data into text
A Python program in "A book that gently teaches difficult programming"
A general-purpose program that formats Linux command strings in python
Newcomer training program by Python
Create a python numpy array
[Python] A program that finds the most common bird types
A Python program that aggregates time usage from icalendar data
[Python] A program that compares each element of list one by one and wins or loses. zip ()
[Ev3dev] Create a program that captures the LCD (screen) using python
A program that determines whether a number entered in Python is a prime number
[Python algorithm] A program that outputs Sudoku answers from a depth-first search
[Python] A program that rotates the contents of the list to the left
Prime number generation program by Python
When writing a program in Python
[Python] A program that calculates the number of chocolate segments that meet the conditions
[Python] A program that calculates the number of socks to be paired
[Python memo] Be careful when creating a two-dimensional array (list of lists)
A python program that resizes a video and turns it into an image
[Ev3dev] Let's make a remote control program by Python with RPyC protocol
[Python] Try to make a sort program by yourself. (Selection sort, insertion sort, bubble sort)
How to sort by specifying a column in the Python Numpy array.
I made a function to see the movement of a two-dimensional array (Python)
I made a program in Python that reads CSV data of FX and creates a large amount of chart images
Create an application that inputs, displays, and deletes forms by using an array as a DB with Python / Flask.
I made a payroll program in Python!
Write a Caesar cipher program in Python
What I did with a Python array
[Python] Create a LineBot that runs regularly
A typed world that begins with Python
PGM that automatically creates a walking route
A textbook for beginners made by Python beginners
[python] How to sort by the Nth Mth element of a multidimensional array
[Beginner] What happens if I write a program that runs in php in Python?
How to install a Python library that can be used by pharmaceutical companies
I want to exe and distribute a program that resizes images Python3 + pyinstaller
[Python] A program that finds the minimum and maximum values without using methods
[Python] A program that calculates the difference between the total numbers on the diagonal line.
[Python] A program that calculates the number of updates of the highest and lowest records