英文:
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
withinit
parameter.Base.Fix2
to create a partially computednth
.&&
short-circuit to implementif
logic.|>
pipe operator.- Generator (similar to comprehension) for Dict initialization.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论