代码之家  ›  专栏  ›  技术社区  ›  Tarik

Julia将EnumerableGroupBy转换为数组

  •  0
  • Tarik  · 技术社区  · 4 年前

    我有以下代码,它对一个数字数组执行groupby操作,并返回一个包含数字和相应计数的元组数组:

    using Query
    a = [1, 2, 1, 2, 3, 4, 6, 1, 5, 5, 5, 5]
    key_counts = a |> @groupby(_) |> @map g -> (key(g), length(values(g)))
    collect(key_counts)
    

    是否有方法完成管道中转换类型为key_counts的最后一步 QueryOperators.EnumerableMap{Tuple{Int64, Int64}, QueryOperators.EnumerableIterable{Grouping{Int64, Int64}, QueryOperators.EnumerableGroupBy{Grouping{Int64, Int64}, Int64, Int64, QueryOperators.EnumerableIterable{Int64, Vector{Int64}}, var"#12#17", var"#13#18"}}, var"#15#20"} Vector{Tuple{Int, Int}} 通过将收集操作作为一个衬里直接集成到管道中?

    0 回复  |  直到 4 年前
        1
  •  1
  •   SawyerWelden    4 年前

    这个问题已经澄清了。我的答案不再是一个解决方案,而是提供了额外的信息。

    使用 key_counts |> collect 而不是 collect(key_counts) 在第二条线上工作,但是 |> collect 在管道末端没有,这感觉像是不想要的行为。

    以下回复不再相关

    当我运行你的代码时,我确实收到了 Vector{Tuple{Int, Int}} 作为输出。 我使用的是Julia v1.6.0和Query v1.0.0。

    using Query
    a = [1, 2, 1, 2, 3, 4, 6, 1, 5, 5, 5, 5]
    key_counts = a |> @groupby(_) |> @map g -> (key(g), length(values(g)))
    output = collect(key_counts)
    typeof(output) # Vector{Tuple{Int64, Int64}} (alias for Array{Tuple{Int64, Int64}, 1})