集合set

set集合是无序的,不能通过索引和切片来做一些操作

集合中的元素具有唯一性,不存在两个相同的元素
集合是可变对象

两种定义方法:
    set()
    {}

三种运算:

    s1 & s2       交集
    s1  |  s2       并集
    s1  -  s2       差集

集合的操作

增加:

python 集合的添加有两种常用方法,分别是add和update。
集合add方法:是把要传入的元素做为一个整个添加到集合中,例如:
>>> a = set('boy')
>>> a.add('python')
>>> a
set(['y', 'python', 'b', 'o'])

 # update 批量更新
li = [21, 4, 2, 312]
s3.update(li)
print(s3)
# {33, 2, 4, 11, 21, 22, 312}

删:pop

      集合删除操作方法:remove
set(['y', 'python', 'b', 'o'])
>>> a.remove('python')
>>> a
set(['y', 'b', 'o'])

 # 清除素有内容
s.clear()

改:update

集合update方法:是把要传入的元素拆分,做为个体传入到集合中,例如:
>>> a = set('boy')
>>> a.update('python')
>>> a
set(['b', 'h', 'o', 'n', 'p', 't', 'y'])

查:

 isdisjoint 是否没有交集

 issubset,是否包含于

 issuperset 是否包含

原文地址:https://www.cnblogs.com/mariobear/p/9069273.html