来自python的【set集合总结/frozenset】

set定义集合(set) 是一个无序的不重复的元素序列,能够去重复使用{},或者set()函数穿件,与dict的符号一样。空集合必须使用set(),因为{}是用来创建空字典dictset中的数据一定要是不可变数据,否则报错TypeError: unhashable type: 'list'# all sets1 = {'one',1,12,1,23,23,'one','two',(...
摘要由CSDN通过智能技术生成

set定义

  • 集合(set) 是一个无序的不重复的元素序列,能够去重复,删除重复项
  • 使用{},或者set()函数穿件,与dict的符号一样。
  • 空集合必须使用set(),因为{}是用来创建空字典dict
  • set中的数据一定要是不可变数据,否则报错TypeError: unhashable type: 'list'
# all set
s1 = {
   'one',1,12,1,23,23,'one','two',('he','j')}
print(s1) #{'one', 1, ('he', 'j'), 12, 23, 'two'}
#无顺序输出
# 不能有可变数据?
#s2 = {'1','2',([1,2,3,4])}TypeError: unhashable type: 'list'
#s2 = {1,2,{1:1}}TypeError: unhashable type: 'dict'
#s1 = {1,2,{1,2}}#TypeError: unhashable type: 'set'

s2 = {
   }
print(type(s2))#<class 'dict'>
s3 = set()
print(s3,type(s3))#set() <class 'set'>
  • 集合是无序集合,不记录元素的位置或插入顺序。因此,不支持索引、切片或其他类似于序列的行为。因此没有切片、下标获取元素,以及一些特定的方法append
Like other collections, sets support x in set, len(set), and for x in set. Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.

set 成员运算符 以及 运算

  • set能够使用in/not in 来判断元素是否在集合内

Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. (For other containers see the built-in dict, list, and tuple classes, and the collections module.)
常见的用途包括成员资格测试、从序列中删除重复项,以及计算诸如交集、并集、差分和对称差分等数学操作。(对于其他容器,请参阅内置的dict、list和tuple类,以及collections模块。)

  • 两个集合之间能够进行运算+ - & ^ ,能够对成员进行操作。交集、并集、查分、对称查分(求反)
    - :a -b : a删除b中的元素。b -a :b删除a中的元素。difference
    | : a | b : a 与b 的所有元素,相当于+,但是这里不能使用+ *操作符哦,因为是无序,没有索引,所以不知道 union
    & : a & b :a,b中都有的元素 intersection
    ^ : a ^ b :a、b中只有一方含有的元素。 symmetric_difference(other)
# 只有一方有的元素
s3 = {
   'a','b','c','d','e','i'}
s4 = set('abcfgtrdshssjs')
print(s3 -s4)# {'e'}  s3中s4不包含的元素
print(s4-s3) #{'t', 'j', 's', 'h', 'g', 'f', 'r'}
#s4扣除s3的元素
#print(s3+s4) TypeError: unsupported operand type(s) for +: 'set' and 'set'
# 没有这个功能
#print(s3*3)TypeError: unsupported operand type(s) for *: 'set' and 'int'
print(s3|s4) #s3 s4  所有元素 {'g', 'c', 's', 't', 'f', 'b', 'h', 'e', 'r', 'a', 'j', 'd'}

print(s3 & s4) #{'a', 'd', 'b', 'c'} 都有的元素

print(s3 ^ s4) #{'r', 'e', 'h', 'g', 't', 's', 'f', 'i', 'j'}
# 只有一方有的元素
#s4.append('12')
  • 没有append等方法,因为是无序列表,没有索引,所以对于序列的操作都无效。

set实例操作/frozenset实例操作

len(s)返回元素的长度

-属于内置函数 ,不属于set实例哦

print(len(s3))#6

in / not in 元素是否在集合中

  • in/not in 判断的是单个元素是否在集合中,如果是元组要注意括号,直接使用逗号分隔,会被识别为两句话哦!
# in/not in
s2 = {
   'one',1,12,1,23,23,'one','two',('he','j')}
print('one' in s2) # true
print(('he','j') in s2)  #true
print(1,12 in s2) #1 true

isdisjoint(other) 是否无交集 是否有与其他元素相同的元素

  • 返回bool型,两个数据是交集为空集的时候,返回true,否则返回false
Return True if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set.
#:如果集合中没有与其他元素相同的元素,则返回True,当且仅当它们的交集是空集时,集合是不相交的。
# 就是空集的时候,返回true。
  • set集合拥有isdisjoint(other)方法,other包含所有数据类型,只判断是否与set集合有无重复。
s5 = {
   'a','b','c'}
s6 ={
   'a'}
s7 ={
   'd'}
s8 = [1,2,3]
s9 = ('a','b')
s10 = 'ab'
print(s5.isdisjoint(s6))#false
print(s5.isdisjoint(s7))#true
print(s5.isdisjoint(s8))#true
print(s5.isdisjoint(s9))#false
print(s5.isdisjoint(s10))#false
print(s5.isdisjoint('1'))#true

issubset(other) 是否所有的元素都含有

  • 判断集合的所有元素是否都包含在集合中,返回bool,如果都存在则表示为子集,返回true,否则返回false。
  • x.issubset(other)other可以包含所有的数据元素,但是注意主导元素x必须是set集合.
  • 在使用的时候,忽略数据类型,只是单纯的对元素本身数据进行比较,必须要每一个
  • 相当于 <=
s5 = {
   'a','b','c'}
s6 ={
   'a'}
s7 ={
   'd'}
s8 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值