Python并集是什么意思?

Python (183) 2023-04-22 19:16:14

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

以属于A或属于B的元素为元素的集合成为A与B的并集。

1、概念

并集就是取所有集合的所有元素,如果出现重复的保留一个。使用符号 | 或者 union 方法完成。

2、实例

纯文本
复制到剪贴板
在新窗口中打开代码
EnlighterJS 3 Syntax Highlighter
my_set1={"apple","orange","pear","banana","food"}
my_set2={"apple","orange","pear"}
both=my_set1|my_set2
print(both)
使用union完成。
my_set1={"apple","orange","pear","banana","food"}
my_set2={"apple","orange","pear"}
both=my_set1.union(my_set2)
print(both)
my_set1={"apple","orange","pear","banana","food"} my_set2={"apple","orange","pear"} both=my_set1|my_set2 print(both) 使用union完成。 my_set1={"apple","orange","pear","banana","food"} my_set2={"apple","orange","pear"} both=my_set1.union(my_set2) print(both)
my_set1={"apple","orange","pear","banana","food"}
my_set2={"apple","orange","pear"}

both=my_set1|my_set2
print(both)
使用union完成。
my_set1={"apple","orange","pear","banana","food"}
my_set2={"apple","orange","pear"}

both=my_set1.union(my_set2)
print(both)

以上就是Python对称差集的介绍,希望能对大家有所帮助。更多Python学习指路:python基础教程

THE END

发表回复