本文共 1564 字,大约阅读时间需要 5 分钟。
python 示例
union() method is used to find the union of all sets, this method is called with this set (set1) and other sets (set1, set2, ...) can be supplied as an arguments, method returns the set containing all elements of all sets (common elements repeat only once).
union()方法用于查找所有集合的并集,此方法以该集合( set1 )调用,其他集合( set1,set2,... )可以作为参数提供,方法返回包含所有元素的集合所有集合中的所有元素(常见元素仅重复一次)。
Syntax:
句法:
set1.union(set2, set3, ...)
Parameter(s):
参数:
set2, set3, ... – Here, set2 is required and other sets are optional.
set2,set3,... –这里, set2是必需的,其他集合是可选的。
Return value:
返回值:
The return type of this method is <class 'set'>, it returns the set of the all elements.
该方法的返回类型为<class'set'> ,它返回所有元素的集合。
Example 1:
范例1:
# Python Set union() Method with Example# declaring the setscars_1 = { "Porsche", "Audi", "Lexus"}cars_2 = { "Porsche", "Mazda", "Lincoln"}# finding the union of both setsx = cars_1.union(cars_2)# print the setprint("cars_1: ", cars_1)print("cars_2: ", cars_2)print("x: ", x)
Output
输出量
cars_1: {'Porsche', 'Audi', 'Lexus'}cars_2: {'Porsche', 'Mazda', 'Lincoln'}x: {'Porsche', 'Mazda', 'Lincoln', 'Audi', 'Lexus'}
Example 2:
范例2:
# Python Set union() Method with Example# declaring the setsx = { "ABC", "PQR", "XYZ"}y = { "ABC", "PQR", "XYZ"}z = { "DEF", "MNO", "ABC"}# finding the union of all setsresult = x.union(y,z)# printing the setsprint("x: ", x)print("y: ", y)print("result: ", result)
Output
输出量
x: {'ABC', 'PQR', 'XYZ'}y: {'ABC', 'PQR', 'XYZ'}result: {'PQR', 'MNO', 'DEF', 'ABC', 'XYZ'}
翻译自:
python 示例
转载地址:http://mcvzd.baihongyu.com/