Let's search from the procession

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

Let's search from the procession
Let's cut the face from the image
Let's add it to the environment variable from the command ~
Search for large files on Linux from the command line
Let's turn the air gacha
Remove the frame from the image
I tweeted from the terminal!
Let's guess the development status of the city from the satellite image.
Repeat with While. Scripts to Tweet and search from the terminal
Do a search by image from the camera roll using Pythonista3
Let's touch Google's Vision API from Python for the time being
Django-TodoList② ~ Let's display the details page ~
DJango Memo: From the beginning (preparation)
Existence from the viewpoint of Python
Let's touch on the Go language
Use the Flickr API from Python
Gender is determined from the name.
Get the value from the [Django] Form
Learn from the winning code-Mercari Competition ①-
Let's display the map using Basemap
Get the address from the zip code
Let's decide the winner of bingo
Let's write a program to solve the Rubik's Cube (Part 2: IDA * Search)
Search for synonyms from the word list (csv) using Python Japanese WordNet
Voice processing by deep learning: Let's identify who the voice actor is from the voice