目录索引
- ==集合特点:==
- ==集合用处:==
去重操作:
- ==创建集合:==
实例:
实例2:
- ==集合运算:==
交集:
并集:
补集:
差集:
集合特点:
a = {3,4,5}
for i in a:print(i)
集合用处:
去重操作:
a = [1,2,3,4,5,6,6,6,7,7,8,9,9,9,9,0,0]
b = list(set(a))
print(b)
#结果:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
创建集合:
实例:
a = {1,2,3}
实例2:
#用set关键字来创建的话,必须用字符串,它会自动的帮你拆开
a = set("apple")
print(a)
集合运算:
交集:
a = {1,2,3}
b = {2,3,4,5}
print(a&b)
#结果:{2, 3}
并集:
a = {1,2,3}
b = {2,3,4,5}
print(a|b)
#结果:{1, 2, 3, 4, 5}
补集:
a = {1,2,3}
b = {2,3,4,5}
print(a^b)
#结果:{1, 4, 5}
差集:
a = {1,2,3}
b = {2,3,4,5}
print(a-b)
print(b-a)
#结果:{1}
# {4, 5}
本文链接:https://my.lmcjl.com/post/2550.html
展开阅读全文
4 评论