在 Julia 中获取集合的并集 - union() 和 union!() 方法
union()
是 julia 中的内置函数,用于构造指定集合的并集。
Syntax: union(s, itrs…)
Parameters:
- s: Specified first set.
- itrs: Specified second set.
Returns: It returns the constructed union of the specified sets.
例子:
# Julia program to illustrate
# the use of union() method
# Getting the constructed union of the specified sets.
println(union([5, 10], [15, 20]))
println(union([2, 4], [2, 4, 6]))
println(union([1, 3], 5:7))
println(union(Set([2, 4, 6]), 1:5))
输出:
[5, 10, 15, 20]
[2, 4, 6]
[1, 3, 5, 6, 7]
[4, 2, 6, 1, 3, 5]
联盟!()
union!()
是 julia 中的一个内置函数,用于构造传入集合的并集并用结果覆盖 s。
Syntax:
union!(s::Union{AbstractSet, AbstractVector}, itrs…)
Parameters:
- s: Specified set.
- itrs: Specified iterator.
Returns: It returns the constructed union of passed in sets and overwrite s with the result.
例子:
# Julia program to illustrate
# the use of union !() method
# Getting constructed union of passed
# in sets and overwrite s with the result.
s1 = Set([1, 2, 3, 4]);
union !(s1, 5:7:9);
println(s1)
s2 = Set([1, 2, 3, 4]);
union !(s2, 1:2:3);
println(s2)
输出:
Set([4, 2, 3, 5, 1])
Set([4, 2, 3, 1])