# -*- coding: utf-8 -*-
""
"
@Time : 2021/8/15 19:20
@Author : LYP
@FileName: set_python.py
@SoftWare: PyCharm
"
""
print(
"创建集合"
)
a = {1,
'c'
,1,(1,2,3),
'c'
,
'a'
,
'e'
,
'e'
}
print(a)
set1 = set(
"c.biancheng.net"
)
set2 = set([1,2,3,4,5])
set3 = set((1,2,3,4,5))
print(
"set1:"
,set1)
print(
"set2:"
,set2)
print(
"set3:"
,set3)
print(
"集合之间的常见操作"
)
set4=set([1,2,3,4,5,6,7,8])
set5=set([6,7,8,9,12,23,45,67])
print(set4)
print(set5)
print(
"set4中添加元素10"
,set4.add(10))
print(
"set5中添加元素10"
,set5.add(10))
print(
"set4中删除元素"
,set4.pop())
# discard() 删除特定元素
print(
"set5中删除元素"
,set5.discard(6))
print(
"set4中移除元素10"
,set4.remove(10))
print(
"set5中移除元素"
,set5.remove(10))
print(
"集合常见操作"
)
print(
"set4和set5的交集"
,set4 & set5)
print(
"set4和set5的并集"
,set4 | set5)
print(
"set4和set5的差集"
,set4 - set5)
print(
"set4和set5的对称差集"
,set4 ^ set5)
print(
" 两个集合的并集"
,set4.union(set5))
print(
" 多个集合的差集"
,set4.difference(set5))
print(
" 集合的交集"
,set4.intersection(set5))
print(
"判断两个集合是否包含相同的元素,"
,set4.isdisjoint(set5))
print(
"判断集合是否有某个元素,"
, 7
in
set5)