[PYTHON] Find it in the procession and edit it

Good evening (* ´ω `)

It is an extension of yesterday. Let's raise the difficulty a little more.

For the time being, randomly set integers 0 to 4 Let's randomly select and create an 8-by-8 matrix.

test.py


import numpy as np
arr = np.random.randint(0,4,(8,8))
print(arr)

Execution result.py


[[2 2 3 2 2 3 2 2]
 [3 1 0 2 1 2 3 1]
 [3 0 1 3 3 1 2 1]
 [1 2 1 1 1 2 2 2]
 [1 2 3 2 3 0 3 0]
 [0 2 0 0 0 1 0 3]
 [0 2 3 0 0 2 2 1]
 [3 3 2 3 3 2 2 0]]

For the time being, look for 0 in a randomly constructed matrix. It seems that the description taught by @ LouisS0616 is how to find only one. I think there are other ways, but I'm sorry, it didn't work m (_ _) m. For the time being, I will go straight.

test.py


arr_pt=[]#Make a list of empty.

for i in range(8):
    for j in range(8):
        if arr[i,j] == 0:
            arr_pt.append([i,j])#In the empty list, arr[i,j] ==Enter the coordinates that will be 0.

Execution result.py


[[2 0 2 1 1 0 3 3]
 [3 0 3 3 2 0 2 3]
 [2 2 0 3 0 2 0 2]
 [3 0 2 2 2 2 0 2]
 [3 0 3 0 1 0 1 0]
 [2 2 1 2 1 0 2 3]
 [1 3 2 1 2 0 0 1]
 [3 0 3 2 2 3 3 2]]
#The following list has line breaks for the sake of clarity.(_ _)m
#The coordinates are counted from 0 rows and 0 columns.
[[0, 1], [0, 5], [1, 1], [1, 5], [2, 2], [2, 4], [2, 6], [3, 1], 
[3, 6], [4, 1], [4, 3], [4, 5], [4, 7], [5, 5], [6, 5], [6, 6], [7, 1]]

The python list has no upper limit, so The number of 0s is different even in a randomly generated matrix. All the coordinates are stored, thank you (`) ノ

After that, I will try to extract the coordinate information from the stored list. This time, edit all the rows and columns where 0 is found to 0. For the time being, I will post it briefly, but I will explain it later.

test.py


for k in range(len(arr_pt)):
    for l in range(8):
        arr[arr_pt[k][0],l] = 0
    for m in range(8):
        arr[m,arr_pt[k][1]] = 0

print(arr)

Execution result.py


#before
[[3 2 3 2 2 1 2 2]
 [3 2 3 2 0 3 0 0]
 [3 3 3 0 2 2 0 1]
 [1 3 1 1 3 0 0 1]
 [2 0 1 3 2 0 3 3]
 [0 1 0 0 1 0 3 1]
 [2 0 3 0 1 3 2 2]
 [0 0 3 2 1 3 2 0]]
#after
[[0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]]

It's all 0 (laughs) While adjusting the balance a little I will repost the whole picture.

test.py


import numpy as np

arr = np.random.randint(0,8,(8,8))#Since it is an 8x8 matrix, 0-I decided to choose 8 at random.
print(arr)

arr_pt=[]

for i in range(8):
    for j in range(8):
        if arr[i,j] == 0:
            arr_pt.append([i,j])
            
print(arr_pt)

#arr_pt[0][0]Is arr_pt[0] =If you say X, X[0]That is
#Please read the description with this as a hint
for k in range(len(arr_pt)):
    for l in range(8):
        arr[arr_pt[k][0],l] = 0 # arr_pt[k]Let X be arr[X[0],l]Will be/The value of a specific row is all 0
    for m in range(8):
        arr[m,arr_pt[k][1]] = 0 # arr_pt[k]Let X be arr[m,X[1]]Will be/Specific column values all 0

print(arr)

Execution result.py


#before
[[3 0 6 5 7 7 5 0]
 [5 5 1 7 5 0 7 7]
 [4 5 2 2 2 7 1 3]
 [5 5 7 0 3 3 0 7]
 [0 3 0 4 7 1 1 0]
 [3 3 3 7 6 7 1 7]
 [3 1 4 4 5 5 7 7]
 [5 2 6 0 2 4 3 3]]

# 0 address (= arry_pt)
[[0, 1], [0, 7], [1, 5], [3, 3], [3, 6], [4, 0], [4, 2], [4, 7], [7, 3]]

#after
[[0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 2 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 0 0 0 6 0 0 0]
 [0 0 0 0 5 0 0 0]
 [0 0 0 0 0 0 0 0]]

The image of the execution result looks like this. 図1.PNG

Personally, I was worried after finding the 0 coordinate and putting it in the list. For example, from the following execution result, for example, arr_pt [0]. ..

Execution result.py


[[0, 1], [0, 7], [1, 5], [3, 3], [3, 6], [4, 0], [4, 2], [4, 7], [7, 3]] # arr_pt
[0, 1]# arr_pt[0]

It will be. It's good to retrieve the coordinates, I didn't know how to reflect it in the elements of an 8x8 matrix. As I wrote briefly, arr_pt [0] [0] will extract 0 from [0,1]. Conversely, arr_pt [0] [1] will extract 1 from [0,1].

it's interesting.

After that, I feel that it is not necessary to use for so far. I'm sorry, I can't think of a good idea. ..

If you are an expert, please give me some advice. m (_ _) m

Recommended Posts

Find it in the procession and edit it
Find the difference in Python
Read the csv file and display it in the browser
Find the Hermitian matrix and its eigenvalues in Python
Scraping the schedule of Hinatazaka46 and reflecting it in Google Calendar
What's in the parameter? Edit String & Expression
Django ~ Let's display it in the browser ~
Find and check inverse matrix in Python
Predict the amount of electricity used in 2 days and publish it in CSV
[Python] Sweet Is it sweet? About suites and expressions in the official documentation
Enclose the cat result in double quotes and put it in a variable
The one that divides the csv file, reads it, and processes it in parallel
I set the environment variable with Docker and displayed it in Python
Scraping the holojour and displaying it with CLI
[Python] Precautions when retrieving data by scraping and putting it in the list
12. Save the first column in col1.txt and the second column in col2.txt
Save the specified channel ID in text and load it at the next startup
About the difference between "==" and "is" in python
Find the number of days in a month
Find the divisor of the value entered in python
Define a division value in Django and easily reflect it on the screen
Find the solution of the nth-order equation in python
POST JSON in Python and receive it in PHP
[Python] Find the transposed matrix in a comprehension
In bash, "Delete the file if it exists".
Explanation of edit distance and implementation in Python
python xlwings: Find the cell in the last row
If you define a method in a Ruby class and define a method in it, it becomes a method of the original class.
Edit and debug the code in the Raspberry Pi with VS Code's SSH connection feature
The result of making a map album of Italy honeymoon in Python and sharing it
[Selenium] Open the link in a new tab and move it [Python / Chrome Driver]
I want to replace the variables in the python template file and mass-produce it in another file.
How to find out which process is using the localhost port and stop it
It is easy to execute SQL with Python and output the result in Excel
The simplest Python memo in Japan (classes and objects)
Investigate the relationship between TensorFlow and Keras in transition
Extract and list personal names and place names in the text
Receive the form in Python and do various things
Carefully understand the exponential distribution and draw in Python
Plot and understand the multivariate normal distribution in Python
Check if it is Unix in the scripting language
Find the part that is 575 from Wikipedia in Python
Carefully understand the Poisson distribution and draw in Python
Select the required variables in TensorFlow and save / restore
Automatically access the flow in enebular and pull the trigger
POST the image with json and receive it with flask
How to use Decorator in Django and how to make it
Check if it is Unix in the scripting language
Find the maximum Python
Edit fonts in Python
Recursively get the Excel list in a specific folder with python and write it to Excel.
Find eigenvalues and eigenvectors
It was great to edit the Python file in the Raspberry Pi with Atom's remote function
List find in Python
If you want to put an argument in the closure function and execute it later
An easy way to view the time taken in Python and a smarter way to improve it
Put Ubuntu in Raspi, put Docker on it, and control GPIO with python from the container
Edit the config file and run docker-compose up (Django + MySQL ④)
Try to make it using GUI and PyQt in Python
Maya | Find out the number of polygons in the selected object
Save the pystan model and results in a pickle file