首页 > 编程语言 > 详细

PythonStudy——集合 Set

时间:2019-04-18 22:04:31      阅读:145      评论:0      收藏:0      [点我收藏+]

# 空集合:不能用{},因为用来标示空字典

s = set()
print(s, type(s))

 

# 概念:
# 1.set为可变类型 - 可增可删
# 2.set为去重存储 - set中不能存放重复数据
# 3.set为无序存储 - 不能索引取值
# 4.set为单列容器 - 没有取值的key
# 总结:set不能取值

 

# 增

s.add(1)
s.add(2)
s.add(1)
print(s)
s.update({2, 3})
print(s)

 

# 删

res = s.pop()
print(res)
s.remove(1)
print(s)
s.clear()
print(s)

 

# set运算
# 交集:两个都有 &

py = {a, b, c, egon}
lx = {x, y, z, egon}
print(py & lx)
print(py.intersection(lx))

 

# 合集:两个的合体 |

print(py | lx)
print(py.union(lx))

 

# 对称交集:抛出共有的办法的合体 ^

print(py ^ lx)
print(py.symmetric_difference(lx))

 

# 差集:独有的

print(py - lx)
print(py.difference(lx))

 

# 比较:前提一定是包含关系

s1 = {1, 2}
s2 = {2}
print(s1 < s2)

 

技术分享图片

 

PythonStudy——集合 Set

原文:https://www.cnblogs.com/tingguoguoyo/p/10732596.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!