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.
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