Tìm trong mảng Python

Hôm nay trong hướng dẫn này, chúng ta sẽ tìm kiếm lần xuất hiện đầu tiên, cuối cùng và tất cả các lần xuất hiện của một phần tử trong một mảng với sự trợ giúp của Đệ quy

Trước khi đi vào bất kỳ báo cáo vấn đề nào, trước tiên chúng ta hãy hiểu Đệ quy là gì. Nếu bạn muốn tìm hiểu về Đệ quy, một liên kết được cung cấp để biết về Đệ quy

Tìm hiểu về Đệ quy tại đây. Đệ quy Python

Tìm sự xuất hiện đầu tiên của phần tử

Hãy bắt đầu bằng cách tìm kiếm sự xuất hiện đầu tiên của phần tử trong một mảng Python. Chúng tôi mong muốn tìm vị trí đầu tiên mà phần tử xuất hiện trong danh sách các phần tử [mảng]

Ví dụ
Mảng đã cho ==> [1,2,3,4,2]
Lần xuất hiện đầu tiên ==> 2

Để tìm giải pháp cho vấn đề, chúng tôi sẽ thực hiện các bước sau

Step 1 :  Check if list is empty then return that list is empty
Step 2 : Check if there is only one element then check the first element with X and return the answer if found
Step 3 : For more than one element, we will check if the first element is equal to X if found then return 
Step 4 : Otherwise recursively go by slicing the array and incrementing and decremementing the itrerator and n value [size of array ] respectively
Step 5 :  Repeat until the element is found or not

Việc triển khai mã của các bước nêu trên được hiển thị bên dưới

def find_first[arr,n,x,itr]:

    # check if list is empty
    if[n==0]:
        print["List empty!"]
        return

    # Only one element
    elif[n==1]:
        if[arr[0]==x]:
            print["Element present at position 1"]
        else:
            print["Element not found"]
        return

    # More than one element
    else:
        if[arr[0] == x]:
            print["Found at position: ", itr+1]
        else:
            find_first[arr[1:],n-1,x,itr+1]
        return

arr = [1,2,3,4,5,2,10,10]
n  = len[arr]
x = 10
itr = 0
find_first[arr,n,x,itr]

đầu ra

Found at position:  7

Tìm lần xuất hiện cuối cùng của một đối tượng

Tiếp theo, chúng tôi sẽ cố gắng tìm lần xuất hiện cuối cùng của phần tử bằng Python. Chúng tôi mong muốn tìm vị trí cuối cùng mà phần tử xuất hiện trong danh sách các phần tử [mảng]

Ví dụ
Mảng đã cho ==> [1,2,3,4,2]
Lần xuất hiện cuối cùng ==> 5

Để tìm giải pháp cho vấn đề, chúng tôi sẽ thực hiện các bước sau

Step 1 :  Check if list is empty then return that list is empty
Step 2 : Check if there is only one element then check the first element with X and return the answer if found
Step 3 : For more than one element, we will check if the last element is equal to X if found then return 
Step 4 : Otherwise recursively go by slicing the array and decremementing both the iterator and n value [size of array ] 
Step 5 :  Repeat until the element is found or not

Thực hiện các bước trên trong Python

def find_first[arr,n,x,itr]:

    # check if list is empty
    if[n==0]:
        print["List empty!"]
        return

    # Only one element
    elif[n==1]:
        if[arr[0]==x]:
            print["Element present at position 1"]
        else:
            print["Element not found"]
        return

    # More than one element
    else:
        if[arr[n-1] == x]:
            print["Found at position: ", itr+1]
        else:
            find_first[arr[:-1],n-1,x,itr-1]
        return

arr = [1,2,3,4,5,2,3,2,3,2,10,10]
n  = len[arr]
x = 2
itr = n - 1
find_first[arr,n,x,itr]

đầu ra

Found at position:  10

Tìm tất cả các lần xuất hiện của một đối tượng

Ở đây chúng tôi hướng đến việc tìm tất cả các vị trí mà phần tử xuất hiện trong danh sách các phần tử [mảng]. Các lần xuất hiện bao gồm vị trí đầu tiên, cuối cùng và bất kỳ vị trí chính giữa nào của phần tử trong mảng

Ví dụ
Mảng đã cho ==> [1,2,3,4,2]
Tất cả các lần xuất hiện ==> 2 5

Để tìm giải pháp cho vấn đề, chúng tôi sẽ thực hiện các bước sau

Step 1 :  Check if list is empty then return that list is empty
Step 2 : Check if there is only one element then print the position of the element and return
Step 3 : For more than one element, we will check if the first element is equal to X if found then print and keep on recursively calling the function again by slicing the array and decremementing n value [size of array ] and incrementing the value of iterator
Step 5 :  Repeat until all the elements are encountered.

Thực hiện các bước trên trong Python

def find_first[arr,n,x,itr]:

    # check if list is empty
    if[n==0]:
        print["List empty!"]
        return

    # Only one element
    elif[n==1]:
        if[arr[0]==x]:
            print[itr+1,end=" "]
        else:
            print["Element not found"]

    # More than one element
    else:
        if[arr[0] == x]:
            print[itr+1,end=" "]
        find_first[arr[1:],n-1,x,itr+1]

arr = [1,2,10,3,4,10,5,2,10,2,3,10]
n  = len[arr]
x = 10
itr = 0
print["Found at position: ",end=""] 
find_first[arr,n,x,itr]

đầu ra

________số 8_______

Phần kết luận

Vì vậy, khi kết thúc hướng dẫn này, chúng ta đã quen với việc tìm các phần tử xuất hiện đầu tiên, cuối cùng và tất cả các phần tử trong một mảng đã cho. Hy vọng bạn hiểu logic

Chủ Đề