python-set详解

一、概述

  set是一个无序的、不重复的序列。

二、集合的创建

  1、set()关键字创建

  2、{}创建,但是该方式不能创建空集合,因为{}是创建字典的

  

三、集合的特性

  3.1 无序、不重复,所以我们可以用来去重

  3.2 集合中的元素必须是可哈希 即不可变类型字符串、整型、元组,可变类型列表是不可以哈希

  3.3 有两种集合,

    set 可变集合 本身是不可哈希

    frozenset 不可变集合

input:
s1 = {1,2,3,4,5}    # {}方法创建
s2 = set([1,2,3,4,5])   # set方法创建
s3 = set('hello')   #遍历后,添加到集合中,并去重
s4 = set(['hello', 'world', 'hello'])
s5 = set([1,2,3,4,5,6,6])
print(s1)
print(s2)
print(s3)
print(s4)
print(s5)

output:
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}
{'e', 'h', 'o', 'l'}
{'world', 'hello'}
{1, 2, 3, 4, 5, 6}

 

四、集合的基本操作

  1、添加

    1.1 add添加

    将元素 x (可以是整数不可迭代数据类型)整体添加到集合 s 中,如果元素已存在,则不进行任何操作。

 

input:

s1 = set(['1','2','3','4','5','6'])
print('添加前:',s1)
s1.add('67')
print('添加后:',s1)
output: 

添加前: {'6', '1', '5', '4', '2', '3'}
添加后: {'6', '1', '5', '4', '2', '67', '3'}



    1.2、update

    将列表,元组,字典等遍历添加到集合中,并去重

input:
s1 = set(['1','2','3','4','5','6'])
print('添加前:',s1)
s1.update('67')
# s1.update(67)   # TypeError: 'int' object is not iterable
print('添加后:',s1)

output:
添加前: {'4', '6', '5', '2', '1', '3'}
添加后: {'4', '6', '5', '2', '1', '7', '3'}

  2、删除

    2.1、remove( x )

    将元素 x 从集合 s 中移除,如果元素不存在,则会发生错误。

    

input:
s1 = set(['1','2','3','4','5','6'])
print('删除前:',s1)
s1.remove('1')
# s1.remove('1')  # 如果元素不存在,则报错
print('删除后:',s1)

output:
删除前: {'2', '5', '3', '4', '6', '1'}
删除后: {'2', '5', '3', '4', '6'}

    2.2 discard( x )

    移除集合中的元素,且如果元素不存在,不会发生错误

input:
s1 = set(['1','2','3','4','5','6'])
print('删除前:',s1)
s1.discard('1')
s1.discard('7') # 删除不存在的元素
print('删除后:',s1)

output:
删除前: {'2', '6', '4', '3', '1', '5'}
删除后: {'2', '6', '4', '3', '5'}

    2.3、pop() 

    随机删除集合中的一个元素

  

input:
s1 = set(['1','2','3','4','5','6'])
print('删除前:',s1)
s1.pop()
print('删除后:',s1)    #测试多次,每次删除的元素不一定

output:
删除前: {'3', '6', '4', '1', '2', '5'}
删除后: {'6', '4', '1', '2', '5'}

    2.4、clear()

    清空集合中的元素,但是集合仍存在

input:
s1 = set(['1','2','3','4','5','6'])
print('清空前:',s1)
s1.clear()
print('清空后:',s1)

output:
清空前: {'1', '5', '2', '3', '6', '4'}
清空后: set()

    2.5、del

    将集合删除

  

input:
s1 = set(['1','2','3','4','5','6'])
print('删除前:',s1)
del(s1)
print('删除后:',s1)

output:
清空前: {'2', '3', '5', '1', '4', '6'}
Traceback (most recent call last):
  File "D:/pythonproject/work/venv/day19/集合.py", line 64, in <module>
    print('清空后:',s1)
NameError: name 's1' is not defined

 

五、集合的关系运算

  1、交集

    1.1、s1.intersection(2)方法

    1.2、&运算符

    返回s1、s2两个集合相同元素的集合,有两种方法

 

input:
s1 = set(['1', '2', '3', '4', '5', '6'])
s2 = set(['5', '6', '7', '8', '9', '10'])
print(s1.intersection(s2))
print(s1 & s2)

output:
{'5', '6'}
{'5', '6'}

 

  

  2、并集

    2.1、s1.union(s2)

    2.2、| 运算符

    返回s1、s2两个集合合并,去重后的集合

 

input:
s1 = set(['1', '2', '3', '4', '5', '6'])
s2 = set(['5', '6', '7', '8', '9', '10'])
print(s1.union(s2))
print(s1 | s2)

output:
{'9', '8', '1', '10', '2', '5', '7', '6', '3', '4'}
{'9', '8', '1', '10', '2', '5', '7', '6', '3', '4'}

 

  3、差集

    3.1、s1.difference(s2)

    3.2、s1 - s2

    返回s1集合中存在但不存在s2集合中的元素的集合

input:
s1 = set(['1', '2', '3', '4', '5', '6'])
s2 = set(['5', '6', '7', '8', '9', '10'])
print(s1.difference(s2))
print(s1 - s2)

output:
s1 = set(['1', '2', '3', '4', '5', '6'])
s2 = set(['5', '6', '7', '8', '9', '10'])
print(s1.difference(s2))
print(s1 - s2)

 

  4、对称交集(交集的反集) 

    4.1、s1.symmetric_difference(s2)

    4.2、s1 ^ s2

    返回去除s1、s2的相同的元素后,分别取相同的元素的集合

  

input:
s1 = set(['1', '2', '3', '4', '5', '6'])
s2 = set(['5', '6', '7', '8', '9', '10'])
print(s1.symmetric_difference(s2))
print(s1 ^ s2)

output:
{'10', '7', '8', '1', '4', '9', '2', '3'}
{'10', '7', '8', '1', '4', '9', '2', '3'}

 

   5、父、子集

    4.1、s1.issuperset(s2)

    判断s1是否是s2的父集(s1包含s2),即s1>s2,返回布尔值

    4.2、s2.issubset(s1)

    判断s2是否是s1的子集(s2被包含s1),即s2<s1,返回布尔值

  

input:
s1 = set(['1', '2', '3', '4', '5'])
s2 = set(['1', '2', '3', '4'])
s3 = set(['1', '2', '3', '7']) #s1中要包含s3中所有的元素,有一个不存在都返回的是False
print(s1.issuperset(s2))
print(s2.issubset(s1))
print(s1 > s2)
print(s2 < s1)
print(s1 > s3)
output: 

True
True
True
True
False

 

 

六、集合内置方法完整列表

方法描述
add() 为集合添加元素
clear() 移除集合中的所有元素
copy() 拷贝一个集合
difference() 返回多个集合的差集
difference_update() 移除集合中的元素,该元素在指定的集合也存在。
discard() 删除集合中指定的元素
intersection() 返回集合的交集
intersection_update() 返回集合的交集。
isdisjoint() 判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False。
issubset() 判断指定集合是否为该方法参数集合的子集。
issuperset() 判断该方法的参数集合是否为指定集合的子集
pop() 随机移除元素
remove() 移除指定元素
symmetric_difference() 返回两个集合中不重复的元素集合。
symmetric_difference_update() 移除当前集合中在另外一个指定集合相同的元素,并将另外一个指定集合中不同的元素插入到当前集合中。
union() 返回两个集合的并集
update() 给集合添加元素

 

 

 

 

 

 

 

 

posted on 2019-11-05 15:20  别动我的锅  阅读(1186)  评论(0)    收藏  举报

导航

levels of contents