如何通过以下方式将一组数组折叠为包含所有唯一的set元素的单个set?
Array_of_sets = Set{String}[Set(["EUR", "GBP", "USD"]), Set(["AUD"])]
# do something to Array_of_sets which produces
Set{String}(["EUR", "GBP", "USD", "AUD"])
union
,vcat
和unique
在这种情况下似乎不起作用。
答案 0 :(得分:7)
所以集合已经是唯一的:
julia> S = Set(["a", "b"])
Set(["b", "a"])
julia> push!(S, "a")
Set(["b", "a"])
因此,不需要unique
:
julia> A = Set{String}[Set(["EUR", "GBP", "USD"]), Set(["EUR", "AUD"])]
julia> reduce(union!, A)
Set(["EUR", "GBP", "AUD", "USD"])
答案 1 :(得分:0)
我会将集合转换为数组,并对其进行vcat处理:
Array_of_sets = Set{String}[Set(["EUR", "GBP", "USD"]), Set(["AUD"])]
Array_of_arrays = map(collect, Array_of_sets)
Set(vcat(Array_of_arrays...))
> Set{String}(["EUR", "GBP", "USD", "AUD"])
编辑:但是那时我不像@stillearningsomething
那样聪明。