在Julia中如何使用 -> 操作符来根据两个索引应用过滤器?

huangapple go评论58阅读模式
英文:

how to use -> operand in julia to apply filter based on two index?

问题

Here's the translation of the code portion:

A = [(3, 4, 5, "s2", "s2"), (2, 4, 5, "s1", "s2"), (3, 4, 6, "s2", "s2"), (1, 4, 6, "s2", "s3"), (1, 3, 6, "s3", "s3")]

# Filter tuples where fourth and fifth elements are the same
same_forth_and_fifth = filter(x -> x[4] == x[5], A)

# Group them based on the fourth element
grouped_same = Dict{String, Vector{Tuple{Int, Int, Int, String, String}}}()
for tup in same_forth_and_fifth
    key = tup[4]
    if haskey(grouped_same, key)
        push!(grouped_same[key], tup)
    else
        grouped_same[key] = [tup]
    end
end

# Filter tuples where fourth and fifth elements are different
different_forth_and_fifth = filter(x -> x[4] != x[5], A)

# Group them based on the fourth element
grouped_different = Dict{String, Vector{Tuple{Int, Int, Int, String, String}}}()
for tup in different_forth_and_fifth
    key = tup[4]
    if haskey(grouped_different, key)
        push!(grouped_different[key], tup)
    else
        grouped_different[key] = [tup]
    end
end

# Print the results
println("This is for when fourth and fifth are the same:")
for (key, value) in grouped_same
    println("$key$value")
end

println("\nThis is for when fourth and fifth are different:")
for (key, value) in grouped_different
    println("$key$value")
end

Please note that this code assumes you have a list of tuples A, and it filters and groups them based on whether the fourth and fifth elements are the same or different, and then prints the results.

英文:

I have a list of tuples like A = [(3, 4, 5, "s2", "s2") , (2, 4, 5, "s1", "s2"), (3, 4, 6, "s2", "s2"),(1, 4, 6, "s2", "s3"),(1, 3, 6, "s3", "s3"),....]

I want to filter out all tuples that their forth and fifth elements are the same, and group them baes on the forth elements.
And also find all tuples that their forth and fifth are not the same, and again group them based on the forth elements.

What I, looking for is like the following. This is for when fifth and forth are the same:

"s1" │ []
"s2" │ [(3, 4, 5, "s2", "s2"), (3, 4, 6, "s2", "s2")]
"s3" │ [(1, 3, 6, "s3", "s3")]

How to achieve this via Julia?

答案1

得分: 2

以下是翻译好的内容:

可能的一种方式是:
```julia
foldl((r,e)->(e[4]==e[5] && push!(r[e[4]],e); r),A ;
  init=Dict(k=>eltype(A)[] for k in 
    Base.Fix2(nth,4).(A) |> unique |> sort))

给出以下输出:

julia> A = [(3, 4, 5, "s2", "s2") , (2, 4, 5, "s1", "s2"), (3, 4, 6, "s2", "s2"),(1, 4, 6, "s2", "s3"),(1, 3, 6, "s3", "s3")];

julia> foldl((r,e)->(e[4]==e[5] && push!(r[e[4]],e); r),A ;init=Dict(k=>eltype(A)[] for k in Base.Fix2(nth,4).(A) |> unique |> sort))
Dict{String, Vector{Tuple{Int64, Int64, Int64, String, String}}} with 3 entries:
  "s1" => []
  "s2" => [(3, 4, 5, "s2", "s2"), (3, 4, 6, "s2", "s2")]
  "s3" => [(1, 3, 6, "s3", "s3")]

使用了一些 Julia 的基本特性:

  • 带有 init 参数的 foldl
  • 使用 Base.Fix2 创建了一个部分计算的 nth
  • 使用 && 短路操作符来实现 if 逻辑。
  • 使用 |> 管道操作符。
  • 使用生成器(类似于推导式)初始化了字典。
英文:

One way might be:

foldl((r,e)->(e[4]==e[5] && push!(r[e[4]],e); r),A ;
  init=Dict(k=>eltype(A)[] for k in 
    Base.Fix2(nth,4).(A) |> unique |> sort))

Giving the following output:

julia> A = [(3, 4, 5, "s2", "s2") , (2, 4, 5, "s1", "s2"), (3, 4, 6, "s2", "s2"),(1, 4, 6, "s2", "s3"),(1, 3, 6, "s3", "s3")];

julia> foldl((r,e)->(e[4]==e[5] && push!(r[e[4]],e); r),A ;init=Dict(k=>eltype(A)[] for k in Base.Fix2(nth,4).(A) |> unique |> sort))
Dict{String, Vector{Tuple{Int64, Int64, Int64, String, String}}} with 3 entries:
  "s1" => []
  "s2" => [(3, 4, 5, "s2", "s2"), (3, 4, 6, "s2", "s2")]
  "s3" => [(1, 3, 6, "s3", "s3")]

A few Julia base features are used:

  • foldl with init parameter.
  • Base.Fix2 to create a partially computed nth.
  • && short-circuit to implement if logic.
  • |> pipe operator.
  • Generator (similar to comprehension) for Dict initialization.

huangapple
  • 本文由 发表于 2023年4月17日 17:31:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定