Good evening ('Д') Thank you for your support m (_ _) m
I've written various algorithms, I wanted to play in a line for a short break. I'll start with the easy guy.
test.py
import numpy as np
Tarray = np.arange(12)
print(Tarray)
Execution result.py
[ 0 1 2 3 4 5 6 7 8 9 10 11]
Can this be changed to, for example, 3 rows and 4 columns? Yes you can.
test.py
import numpy as np
Tarray = np.arange(12).reshape(3,4)
print(Tarray)
Execution result.py
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
I see, it's interesting !! From here, to print only specific elements What should i do? How about such a description? ..
test.py
import numpy as np
Tarray = np.arange(12).reshape(3,4)
print(Tarray)
print()
print(f"Tarray[0,0] is {Tarray[0,0]}")
Execution result.py
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Tarray[0,0] is 0
I see. (´ ー `) y- ~~ Once you know this far You may be able to find the number you want from the procession. Let's do it (≧ ▽ ≦)
For example, data from the first row of 2 rows and 10 columns Wouldn't it be like this when extracting?
test.py
import numpy as np
Tarray = np.arange(20).reshape(2,10)
print(Tarray)
num = int(input(": "))
for i in range(10):
if Tarray[0,i] == num:
print(f"founded data is placed Tarray[0,{i}] ")
Execution result.py
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]]
: 6
founded data is placed Tarray[0,6]
Next, let's search across lines. It's OK if you change 0 in Tarray [0, i] above to a variable.
test.py
import numpy as np
Tarray = np.arange(20).reshape(2,10)
print(Tarray)
num = int(input(": "))
for j in range(2):
for i in range(10):
if Tarray[j,i] == num:
print(f"founded data is placed Tarray[{j},{i}] ")
Execution result.py
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]]
: 13
founded data is placed Tarray[1,3]
But what if even rows and columns become variables? I wrote it like this.
matrix_search.py
import numpy as np
M,N = map(int,input("enter M , N: ").split())
atest = np.arange(M*N).reshape(M,N)
print(atest)
num = int(input("find value: "))
for i in range(M):
for j in range(N):
if atest[i,j] == num:
print(f"found value is placed at [{i},{j}]")
Execution result.py
enter M , N: 4 8
[[ 0 1 2 3 4 5 6 7]
[ 8 9 10 11 12 13 14 15]
[16 17 18 19 20 21 22 23]
[24 25 26 27 28 29 30 31]]
find value: 20
found value is placed at [2,4]
The position display is configured as having 0 rows and 0 columns. There is no such thing (laugh)
matrix_search.py
import numpy as np
M,N = map(int,input("enter M , N: ").split())
atest = np.arange(M*N).reshape(M,N)
print(atest)
num = int(input("find value: "))
for i in range(M):
for j in range(N):
if atest[i,j] == num:
print(f"found value is placed at [{i+1},{j+1}]")
Execution result.py
enter M , N: 4 8
[[ 0 1 2 3 4 5 6 7]
[ 8 9 10 11 12 13 14 15]
[16 17 18 19 20 21 22 23]
[24 25 26 27 28 29 30 31]]
find value: 20
found value is placed at [3,5]
Yes, this time it looks okay. See you again ^^) _ Dan ~~
Recommended Posts