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

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

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

问题

Here's the translation of the code portion:

  1. 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")]
  2. # Filter tuples where fourth and fifth elements are the same
  3. same_forth_and_fifth = filter(x -> x[4] == x[5], A)
  4. # Group them based on the fourth element
  5. grouped_same = Dict{String, Vector{Tuple{Int, Int, Int, String, String}}}()
  6. for tup in same_forth_and_fifth
  7. key = tup[4]
  8. if haskey(grouped_same, key)
  9. push!(grouped_same[key], tup)
  10. else
  11. grouped_same[key] = [tup]
  12. end
  13. end
  14. # Filter tuples where fourth and fifth elements are different
  15. different_forth_and_fifth = filter(x -> x[4] != x[5], A)
  16. # Group them based on the fourth element
  17. grouped_different = Dict{String, Vector{Tuple{Int, Int, Int, String, String}}}()
  18. for tup in different_forth_and_fifth
  19. key = tup[4]
  20. if haskey(grouped_different, key)
  21. push!(grouped_different[key], tup)
  22. else
  23. grouped_different[key] = [tup]
  24. end
  25. end
  26. # Print the results
  27. println("This is for when fourth and fifth are the same:")
  28. for (key, value) in grouped_same
  29. println("$key$value")
  30. end
  31. println("\nThis is for when fourth and fifth are different:")
  32. for (key, value) in grouped_different
  33. println("$key$value")
  34. 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:

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

How to achieve this via Julia?

答案1

得分: 2

以下是翻译好的内容:

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

给出以下输出:

  1. 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")];
  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))
  3. Dict{String, Vector{Tuple{Int64, Int64, Int64, String, String}}} with 3 entries:
  4. "s1" => []
  5. "s2" => [(3, 4, 5, "s2", "s2"), (3, 4, 6, "s2", "s2")]
  6. "s3" => [(1, 3, 6, "s3", "s3")]

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

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

One way might be:

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

Giving the following output:

  1. 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")];
  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))
  3. Dict{String, Vector{Tuple{Int64, Int64, Int64, String, String}}} with 3 entries:
  4. "s1" => []
  5. "s2" => [(3, 4, 5, "s2", "s2"), (3, 4, 6, "s2", "s2")]
  6. "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:

确定