python 学习 --集合

什么是集合:

  • 集合是一种数据类型,用于存储多个元素,并确保元素的唯一性。
  • 集合中的元素是无序的,不可通过索引或切片进行访问。
  • 集合的主要特点是元素不重复,相同的元素在集合中只会出现一次。
  • 我们可以使用大括号 {} 或 set() 函数来定义和创建集合。
  • 集合提供了各种集合运算,如并集(两个集合中的所有元素)、交集(两个集合中共有的元素)、差集(第一个集合中存在而第二个集合中不存在的元素)等操作。

创建集合:

区别于字典,列表等类型,不能使用{ }来创建空集合,这样创建的是字典类型,但是可以用{ }方法来创建非空集合;

a = {1 , 2}
a1 = {}
b = set("abc")
print(type(a), a)  # <class 'set'> {1, 2}
print(type(a1), a1)  # <class 'dict'> {}
print(type(b), b)  # <class 'set'> {'b', 'c', 'a'}

集合的元素具有唯一性:

a = {1, 1, 2, 2, 3}
print(a)  # {1, 2, 3}

集合常见的用途包括成员检测、从序列中去除重复项以及数学中的集合类计算,例如交集、并集、差集与对称差集等等。

由于集合不支持下标操作,所以不支持常规方式的获取和修改。

集合操作:

# add() 方法:向集合中添加一个元素,如果元素已经存在,则集合不发生变化,用法:xx.add(ele);

a = {1, 2, 3}
a.add(4)
print(a)  # {1, 2, 3, 4}
a.add(4)
print(a)  # {1, 2, 3, 4}

# update() 方法: 更新集合,添加others中的所有元素,others需要是一个可迭代对象,如果others中的数据在集合中已经存在,则集合不发生变化;

a = {1, 2, 3}
a.update([4])
print(a)  # {1, 2, 3, 4}
a.update(5)
print(a)  # TypeError: 'int' object is not iterable

# pop() 方法:从集合中删除任意一个元素并将其返回,如果集合为空,则报错;

a = {"a", "b", "c"}
print(a.pop())  # a
print(a)  # {'c', 'b'}
b = set()
b.pop()  # KeyError: 'pop from an empty set'

# remove() 方法:从集合中移除指定元素ele,如果指定元素不存在则报错;

a = {"a", "b", "c"}
a.remove("c")
print(a)  # {'a', 'b'}
a.remove("c")  # KeyError: 'c'

# discard()  方法: 如果元素ele存在则将其从集合中移除,若不存在,则不发生变化;

a = {"a", "b", "c"}
a.discard("c")
print(a)  # {'a', 'b'}
a.discard("c")
print(a)  # {'a', 'b'}

# clear() 方法:清空集合;

a = {"a", "b", "c"}
a.clear()
print(a)  # set()

集合数据操作:

# isdisjoint() 方法:判断集合中是否存在与other共有的元素,如果没有则返回True,当且仅当两个集合的交集为空集合时,两者为不相交集合,用法:xxx.isdisjoint(other)

a = {1, 2, 3}
b = {3, 4, 5}
c = {5, 6, 7}
d = set()
print(a.isdisjoint(b))  # False
print(a.isdisjoint(c))  # True
print(a.isdisjoint(d))  # True

# issubset() 方法:判断当前集合中的每一个元素是否都存在于other中,如果是则返回True,即当前集合是不是other的子集,用法: xxx.issubset(other)

a = {3}
b = {3, 4, 5}
c = {5, 6, 7}
d = set()
print(a.issubset(b))  # True
print(a.issubset(c))  # False
print(d.issubset(a))  # True

# issuperset() 方法:判断other集合中的每一个元素是否都存在于当前集合中,如果是则返回True,即other集合是不是当前集合的子集,当前集合是不是other的超集,用法: xxx.issuperset(other)

a = {3}
b = {3, 4, 5}
c = {5, 6, 7}
d = set()
print(b.issuperset(a))  # True
print(b.issuperset(c))  # False
print(b.issuperset(d))  # True

# union() 方法:返回一个新的集合,新集合的元素包括当前集合和others集合中的所有元素,others可以指定多个集合,即生成选中集合的并集,用法:xx.union(*other)

a = {3}
b = {3, 4, 5}
c = {5, 6, 7}
d = set()
e = a.union(b, c, d)
print(e)  # {3, 4, 5, 6, 7}

# intersection() 方法:返回一个新的集合,新集合的元素为当前集合和others集合中的共有元素,同样others可以指定多个集合,即生成选中集合的交集,用法: xx.intersection(*other)

a = {3, 5 ,7}
b = {3, 4, 5}
c = {5, 6, 7}
d = set()
e = b.intersection(a, c)
print(e)  # {5}

# difference() 方法:返回一个新的集合,新集合的元素为当前集合中有,但是others集合没有的元素,others可以指定多个集合,用法:xx.difference(*other)

a = {3, 5, 7}
b = {3, 4, 5}
c = {5, 6, 7}
d = set()
e = b.difference(a, c)
print(e)  # {4}

# symmetric_difference() 方法:返回一个新的集合,新集合中的元素为两个集合的交集之外的剩余元素,即其中的元素或属于原集合或属于 other 指定的其他集合,但不能同时属于两者,用法:xxx.symmetric_difference(other)

a = {3, 5, 7}
b = {3, 4, 5}
c = {5, 6, 7}
d = set()
e = a.symmetric_difference(b)
print(e)  # {4, 7}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值