如何在 Julia 中将新元素追加到 Set 中?对于数组,这可以通过append
来完成A = [1, 2, 3] # Array
S = Set(A) # Set
append!(A, 4) # Works
append!(S, 4) # Does not work
使用
push!
获取未排序的集合
julia> S = Set([1, 2, 3])
Set{Int64} with 3 elements:
2
3
1
julia> push!(S, 4)
Set{Int64} with 4 elements:
4
2
3
1
您可以将
union!
用于集合,其功能与用于数组的 append!
类似:
julia> A = [1, 2, 3]; # Array
julia> S = Set(A); # Set
julia> append!(A, 4); A # Works
4-element Vector{Int64}:
1
2
3
4
julia> union!(S, 4); S # Works
Set{Int64} with 4 elements:
4
2
3
1