在 Julia 中获取过滤后集合的副本 – filter() 和 filter!() 方法
filter()是 julia 中的内置函数,用于返回执行不同类型过滤的指定抽象数组的副本。这些所有操作都通过以下示例进行说明。
Syntax:
filter(f, a::AbstractArray)
or
filter(f, d::AbstractDict)
or
filter(f, itr::SkipMissing{<:AbstractArray})
Parameters:
- f: Specified instruction.
- a::AbstractArray: Specified array.
- d::AbstractDict: Specified dictionary.
- itr::SkipMissing: Specified SkipMissing iterator.
Returns: It returns a copy of the specified abstract array with different types of filter operation.
示例 1:此示例返回a的副本,删除 f 为假的元素。
Python
# Julia program to illustrate
# the use of filter() method
# Getting a copy of specified array,
# removing elements for which f is false.
a = 5:20
println(filter(isodd, a))
println(filter(iseven, a))
Python
# Julia program to illustrate
# the use of filter() method
# Getting a copy of dictionary, removing
# elements for which f is false
A = Dict(1 =>"a", 2 =>"b", 3 =>"c")
println(filter(p->isodd(p.first), A))
println(filter(p->iseven(p.first), A))
Python
# Julia program to illustrate
# the use of filter() method
# Getting a vector similar to the array
# wrapped by the given SkipMissing
# iterator but with all missing elements
# and those for which f returns false removed.
A = [5 7; missing 8; 10 11]
println(filter(isodd, skipmissing(A)))
println(filter(iseven, skipmissing(A)))
Python
# Julia program to illustrate
# the use of filter !() method
# Getting updated specified array,
# removing elements for which f is false.
a = Vector(2:10)
println(filter !(isodd, a))
b = Vector(2:10)
println(filter !(iseven, b))
Python
# Julia program to illustrate
# the use of filter !() method
# Getting updated dictionary, removing
# elements for which f is false
A = Dict(1 =>"a", 2 =>"b", 3 =>"c")
println(filter !(p->isodd(p.first), A))
B = Dict(1 =>"a", 2 =>"b", 3 =>"c")
println(filter !(p->iseven(p.first), B))
输出:
[5, 7, 9, 11, 13, 15, 17, 19]
[6, 8, 10, 12, 14, 16, 18, 20]
示例 2:此示例返回指定字典的副本,删除 f 为假的元素。
Python
# Julia program to illustrate
# the use of filter() method
# Getting a copy of dictionary, removing
# elements for which f is false
A = Dict(1 =>"a", 2 =>"b", 3 =>"c")
println(filter(p->isodd(p.first), A))
println(filter(p->iseven(p.first), A))
输出:
示例 3:此示例返回一个与给定 SkipMissing 迭代器包装的数组类似的向量,但删除了所有缺失元素和 f 返回 false 的元素。
Python
# Julia program to illustrate
# the use of filter() method
# Getting a vector similar to the array
# wrapped by the given SkipMissing
# iterator but with all missing elements
# and those for which f returns false removed.
A = [5 7; missing 8; 10 11]
println(filter(isodd, skipmissing(A)))
println(filter(iseven, skipmissing(A)))
输出:
筛选!()
filter!()是 julia 中的一个内置函数,用于通过执行不同类型的过滤来更新指定的抽象数组。这些所有操作都通过以下示例进行说明。
Syntax:
filter!(f, a::AbstractArray)
or
filter!(f, d::AbstractDict)
Parameters:
- f: Specified instruction.
- a::AbstractArray: Specified array.
- d::AbstractDict: Specified dictionary.
Returns: It returns the updated specified abstract array with different types of filter operation.
示例 1:此示例返回更新的 a ,删除 f 为假的元素。
Python
# Julia program to illustrate
# the use of filter !() method
# Getting updated specified array,
# removing elements for which f is false.
a = Vector(2:10)
println(filter !(isodd, a))
b = Vector(2:10)
println(filter !(iseven, b))
输出:
[3, 5, 7, 9]
[2, 4, 6, 8, 10]
示例 2:此示例返回更新后的指定字典,删除 f 为假的元素。
Python
# Julia program to illustrate
# the use of filter !() method
# Getting updated dictionary, removing
# elements for which f is false
A = Dict(1 =>"a", 2 =>"b", 3 =>"c")
println(filter !(p->isodd(p.first), A))
B = Dict(1 =>"a", 2 =>"b", 3 =>"c")
println(filter !(p->iseven(p.first), B))
输出: