📜  在数组中搜索 Julia 中的给定元素

📅  最后修改于: 2022-05-13 01:54:27.467000             🧑  作者: Mango

在数组中搜索 Julia 中的给定元素

给定一个数组(1D、2D 或 3D),以及要在数组中查找的元素。如果元素存在于数组中,则打印元素在数组中的位置,否则打印“未找到元素”。
在最近对 Julia 的更新中,开发人员将search()find()函数统一为一个更简单的findall()函数。但是,它的其他对应项,例如findnext()findprev()等仍然可以访问。 find()函数被filter()方法替换,并添加了一个新的indexin()函数,该函数返回数组中特定元素的索引。

例子:

Input : array1 = [ 5, 9, 1, 8, 2, 7 ] 
        sch = 8 
Output : Element found at position 4

Input : array2 = [ 7 9 2
                   9 4 1
                   8 4 6 ]
        sch = 4
Output : Element found at CartesianIndex(2, 2).
         Element found at CartesianIndex(3, 2).

Input : array3 = [ 22, 7, 91, 69, 2, 74 ] 
        sch = 6 
Output : Element not found.

[Note: Unlike other languages, In Julia indexing of an Array starts with 1.]

使用findall()方法

findall()方法用于从数组中查找所需元素的所有出现,并返回索引的数组(或 CartesianArray,取决于输入数组的维度),其中元素存在于输入数组中。如果输入数组中不存在该元素,则返回一个空数组。
句法:

findall(::Function, ::Number)
# Julia program to illustrate the 
# use of findall() method to search 
# for an element in an array.
   
# Searching for an element in a 1D ARRAY
array1 = [ 12, 35, 7, 34, 72, 12, 5 ]
sch = 12
   
indexArray = findall( x -> x == sch, array1 )
for i in indexArray
    println("Element found at position ", i)
end
if (length( findall( x -> x == sch, array1 )) == 0)
    println("Element not found.")
end
   
# Searching for an element in a 2D ARRAY
# of shape (3 x 3).
array2 = [ 23 56 12; 1 7 12; 23 67 34 ]
sch = 7
   
indexArray = findall( x -> x == sch, array2 )
for i in indexArray
    println("Element found at ", i)
end
if (length( findall( x -> x == sch, array2 )) == 0)
    println("Element not found.")
end
    
# Searching for an element in a 3D ARRAY
# of shape (3 x 3 x 3).
array3 = cat([1 2 3; 5 4 2; 3 5 6],
             [1 5 3; 3 1 7; 8 0 1],
             [1 3 6; 3 7 7; 8 8 2], dims=3)
sch = 14
   
indexArray = findall( x -> x == sch, array3 )
for i in indexArray
    println("Element found at ", i)
end
if (length( findall( x -> x == sch, array3 )) == 0)
    println("Element not found.")
end

输出:

使用indexin()方法

indexin()函数用于搜索元素在数组中第一次出现的位置(按行搜索)并返回其在数组中的位置。但是,一旦在数组中找到搜索的元素,搜索就会停止。因此,此函数始终返回长度为 1 的数组。如果在数组中未找到搜索的元素,则在数组中不返回nothing
句法:

indexin(::Any, ::AbstractArray)
# Julia program to illustrate the 
# use of indexin() method to search 
# for an element in an array.
   
# Searching for an element in a 1D ARRAY
array1 = [ 12, 35, 7, 34, 72, 12, 7 ]
sch = 7
   
positionArray = indexin( sch, array1 )
if (positionArray .== nothing )
    println("Element not found.")
else
    println("Element found at position ", 
             positionArray[1])
end
   
# Searching for an element in a 2D ARRAY
# of shape (3 x 3).
array2 = [ 23 56 12; 1 7 12; 23 67 34 ]
sch = 12
   
positionArray = indexin( sch, array2 )
if (positionArray .== nothing )
    println("Element not found.")
else
    println("Element found at ", positionArray[1])
end
   
# Searching for an element in a 3D ARRAY
# of shape (3 x 3 x 3).
array3 = cat([1 2 3; 5 4 2; 3 5 6],
             [1 5 3; 3 1 7; 8 0 1],
             [1 3 6; 3 7 7; 8 8 2], dims=3)
sch = 14
   
positionArray = indexin( sch, array3 )
if (positionArray .== nothing )
    println("Element not found.")
else
    println("Element found at ", positionArray[1])
end

输出:

使用filter()方法

filter()函数通常用于过滤数组中满足某些特定条件的元素。但是,我们也可以使用这个函数来搜索数组中的元素。但是,此函数不是返回索引,而是从数组中返回元素本身。所以,我们可以检查这个函数返回的数组的大小,并相应地打印。
句法:

filter(::Any, ::Array{T, N}) where {T, N} at array.
# Julia program to illustrate the 
# use of filter() method to search 
# for an element in an array.
   
# Searching for an element in a 1D ARRAY
array1 = [ 12, 35, 7, 34, 72, 12, 7 ]
sch = 72
   
elementArray = filter( x -> x == sch, array1 )
if (length(elementArray) == 0)
    println("Element not found.")
else
    println("Element found in the array.")
end
   
# Searching for an element in a 2D ARRAY
# of shape (3 x 3).
array2 = [ 23 56 12; 1 7 12; 23 67 34 ]
sch = 33
   
elementArray = filter( x -> x == sch, array2 )
if (length(elementArray) == 0)
    println("Element not found.")
else
    println("Element found in the array.")
end
   
# Searching for an element in a 3D ARRAY
# of shape (3 x 3 x 3).
array3 = cat([1 2 3; 5 4 2; 3 5 6],
             [1 5 3; 3 1 7; 8 0 1], 
             [1 3 6; 3 7 7; 8 8 2], dims=3)
sch = 3
   
elementArray = filter( x -> x == sch, array3 )
if (length(elementArray) == 0)
    println("Element not found.")
else
    println("Element found in the array.")
end

输出:

除了所有这些在数组中搜索元素的内置函数外,我们还可以使用两种最常见的搜索算法,即线性搜索和二进制搜索。