Python Basic - python set (集合)、集合的操作,增查改、子集,交集,并集,父集,反向交集

文章目录

  • 前言
  • 集合的两大功能:
  • 集合的创建
    • 方法一:通过set()方法进行创建
    • 方法二: 通过列表进行转换成set
    • 测试列表充当集合的元素
  • 集合类型
    • 可变集合(set)
    • 不可变集合(frozenset)
  • 集合的操作
      • add()
      • update()
      • remove()
      • pop()
      • clear()
      • del()
      • discard()
  • 集合的其它操作
    • in 、 not in
    • 集合等价与不等价(==,!=)
    • 子集( issubnet()、<)
    • 并集 (union() / |)
    • 交集(&)/ intersection()
    • 交集更新--intersection_update():交集后将元素覆盖原有集合元素
    • 差集 difference() /(-)
    • 差集更新--difference_update():从A集合中删除B集合的元素
    • 反向交集、对称差集 symmetric_difference() /(^)
    • 对称差集更新--symmetric_difference_update():对称差集后覆盖更新原集合
    • 父集(超集)--issuperset()
    • 集合互斥--isdisjoint():判断A与B是否不相交

前言

  1. 集合:数学中的一种数据规集方法
  2. 把不同的元素组合在一起,这与"列表(List)"不同,列表中可以有重复的元素,只要下标不同,存什么内容都可以,
  3. 但是集合中,相同的元素会去掉,同样的数据只存一份
  4. 集合对象是一组无序排序的可哈希的值:像列表,字典是一种可变的数据类型,所以是不可哈希的,所以不能充当集合的元素
  5. 集合成员可以充当字典的键,因为字典的key也要求不重复,且也是通过哈希存放的。
  6. 集合是无序的,所以没有索引,或者叫下标,而且也不像字典没有键,如果想查值,可以通过循环进行遍历,也可以通过“迭代器”。

集合的两大功能:

  1. 去重
  2. 关系测试,测试两数据之前的交集,差集,并集等关系

集合的创建

方法一:通过set()方法进行创建

set集合只能通过关键字“set()"进行创建,如果是直接通过set()方法创建,里面的字符串中的每个字符都将成为集合中单独的元素。

CreateSet = set("Tesla")
print(CreateSet)

#以下为输出的内容
"""
{'s', 'a', 'T', 'e', 'l'}
"""

可以看到上面的输出,输出为一个集合

方法二: 通过列表进行转换成set

CreateList = list(["Tesla","Elon Mask"])

CreateSet = set(CreateList)

print(CreateSet)
print(type(CreateList))
print(type(CreateSet))
"""
{'Tesla', 'Elon Mask'}


"""

测试列表充当集合的元素

CreateList = list(["Tesla","Elon Mask",[1,2]])
CreateSet = set(CreateList)

print(CreateSet)
print(type(CreateList))
print(type(CreateSet))
"""
TypeError: unhashable type: 'list'
"""

在前言中有描述,列表是可变的数据类型,所以不可哈希,所以不能转换成集合。

集合类型

可变集合(set)

可添加删除元素,但是集合的整体对象是不可哈希的,所以不能当作 字典的键,也不能做其它集合的元素

不可变集合(frozenset)

与可变集合相反

集合的操作

集合与字典一样,是一个无序的序列,是通过哈希后进行存储的,所以没有下标,不能创建索引或者切片操作,但可以使用for循环,in,not in 来访问或者判断集合的元素
返回为布尔类型、

CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)

for i in CreateSet:
    print(i)
"""
Elon Mask
2
Tesla
1
"""

print(2 in CreateSet)
print(3 in CreateSet)
print("Tesla" in CreateSet)

"""
True
False
True
"""

add()

CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)

print(CreateSet)

CreateSet.add("Space X")

print(CreateSet)

"""
{'Elon Mask', 1, 2, 'Tesla'}
{1, 2, 'Tesla', 'Elon Mask', 'Space X'}

"""

但是如果是添加重复的元素,结果只会显示1个,如下所示,添加了3次重复的元素,但是结果只会有一个。

CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)

print(CreateSet)

CreateSet.add("Space X")
CreateSet.add("Space X")
CreateSet.add("Space X")

print(CreateSet)

"""
{'Elon Mask', 1, 2, 'Tesla'}
{1, 2, 'Tesla', 'Elon Mask', 'Space X'}

"""

update()

CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)

print(CreateSet)

CreateSet.update("Solar")
CreateSet.update(["Solar System","Solar Roof","Tesla"])
print(CreateSet)

"""
{1, 2, 'Elon Mask', 'Tesla'}
{1, 2, 'Tesla', 'Solar System', 'o', 'a', 'r', 'Solar Roor', 'S', 'Elon Mask', 'l'}
"""

update是更新的意思,之前的还会存在,不会删除,新的内容增加进去,但是与add不一样的是,add是增加的无论多长当成一个元素,但是update会把字符串当成多个元素添加进去。但是可以使用列表,这样可以一次性添加多个元素,且可以把元素当成一个整体。如果有重复的就去重后再增加

remove()

CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)

print(CreateSet)
CreateSet.remove("Tesla")
print(CreateSet)

"""
{1, 2, 'Elon Mask', 'Tesla'}
{1, 2, 'Elon Mask'}
"""

pop()

CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)

print(CreateSet)
test = CreateSet.pop()
print(CreateSet)
print(test)

"""
{1, 2, 'Elon Mask', 'Tesla'}
{1, 2, 'Elon Mask'}
"""

pop()方法用于随机删除元素,括号内是不能跟参数的,跟上参数后会报错。
但是pop有返回值,所以可以使用一个变量进行接受弹出的值后可以打印出来,

clear()

CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)

print(CreateSet)
CreateSet.clear()
print(CreateSet)

"""
{1, 2, 'Tesla', 'Elon Mask'}
set()
"""

清空,清空后就变成一个空集合了。

del()

CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)

print(CreateSet)
del CreateSet
print(CreateSet)

"""
NameError: name 'CreateSet' is not defined
{'Elon Mask', 2, 'Tesla', 1}
"""

直接删集合了。集合就不存在了

discard()

从集合中删除某个指定的元素,这个元素得是它的集合的成员,如果不是成员则不做任何事情。

        """
        Remove an element from a set if it is a member.
        
        If the element is not a member, do nothing.
        """
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])

CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)

CreateSet.discard(2)
print(CreateSet)

"""
{'Tesla', 1, 'Elon Mask'}
"""

集合的其它操作

in 、 not in

CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)
print(CreateSet)
print("Tesla" in CreateSet)
print("Space X" not in CreateSet)
print(CreateSet)

"""
{1, 2, 'Tesla', 'Elon Mask'}
True
True
{1, 2, 'Tesla', 'Elon Mask'}
"""

集合等价与不等价(==,!=)

CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask"])

CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)

print(CreateSet == CreateSet1)
print(CreateSet1 == CreateSet2)
print(CreateSet1 != CreateSet2)

"""
True
False
True
"""

如果两个集合是等价的则返回true
如果两个集合是不等价的就返回false

子集( issubnet()、<)

CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask"])

CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)

print(CreateSet2.issubset(CreateSet))
print(CreateSet2 < CreateSet)
print(CreateSet1 < CreateSet2)

"""
True
True
False
"""
print(CreateSet2.issubset(CreateSet))     # CreateSet2是否是CreateSet的子集,是就返回Ture,不是就返回False
print(CreateSet2 < CreateSet)				# < 与 issubset() 是相同的功能
print(CreateSet1 < CreateSet2)				# 1与2是等价的,相等的,但是在Python中,并不是子集。

并集 (union() / |)

就是求合集,两个集合合并,相同的元素删除,不同的元素放在一起。
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])

CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)

print(CreateSet.union(CreateSet3))
print(CreateSet | CreateSet3)

"""
{1, 2, 'Tesla', 'Space X', 'Elon Mask'}
{1, 2, 'Tesla', 'Space X', 'Elon Mask'}
"""

交集(&)/ intersection()

CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])

CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)

print(CreateSet.intersection(CreateSet3))
print(CreateSet & CreateSet3)

"""
{'Elon Mask', 'Tesla'}
{'Elon Mask', 'Tesla'}
"""

找出两个集合中相同的元素

交集更新–intersection_update():交集后将元素覆盖原有集合元素

CreateSet 与CreateSet2 先取交集,然后再更新给CreateSet,这个更新是只保留交集的元素
“”" Update a set with the intersection of itself and another. “”"

CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])

CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)

CreateSet.intersection_update(CreateSet2)
# CreateSet 与CreateSet2 先取差集,然后再从CreateSet中Remove掉
print(CreateSet)

"""
{1, 2}
"""

差集 difference() /(-)

跟数学书上讲的一样,A圈里有,B圈里没有的

CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])

CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)

print(CreateSet.difference(CreateSet3))
print(CreateSet - CreateSet3)

print(CreateSet3.difference(CreateSet))
print(CreateSet3 - CreateSet)
"""
{1, 2}
{1, 2}
{'Space X'}
{'Space X'}
"""

差集更新–difference_update():从A集合中删除B集合的元素

CreateSet 与CreateSet2 先取差集,然后再从CreateSet中Remove掉
“”" Remove all elements of another set from this set. “”"

CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])

CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)

CreateSet.difference_update(CreateSet2)
# CreateSet 与CreateSet2 先取差集,然后再从CreateSet中Remove掉
CreateSet1.difference_update(CreateSet3)
print(CreateSet)
print(CreateSet1)

"""
{'Tesla', 'Elon Mask'}
{1, 2}
"""

反向交集、对称差集 symmetric_difference() /(^)

a.symmetric_difference(b) 对称差集,两边都互相没有的。反向交集。
  1. 先做交集
  2. 把交集的元素扣掉
  3. 然后再并起来
  4. (A -B) 并 (B-A) = (A 并 B)- ( A 交 B )
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])

CreateSet = set(CreateList)
CreateSet3 = set(CreateList3)

print(CreateSet.symmetric_difference(CreateSet3))
print(CreateSet ^ CreateSet3)

"""
{1, 2, 'Space X'}
{1, 2, 'Space X'}
"""

对称差集更新–symmetric_difference_update():对称差集后覆盖更新原集合

A与B对称交集后再覆盖更新A
“”" Update a set with the symmetric difference of itself and another. “”"

CreateList = list(["Tesla","Elon Mask",1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])

CreateSet = set(CreateList)
CreateSet3 = set(CreateList3)
CreateSet.symmetric_difference_update(CreateSet3)
print(CreateSet)

"""
{1, 2, 'Space X'}

"""

父集(超集)–issuperset()

CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])

CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)

print(CreateSet.issuperset(CreateSet2))
print(CreateSet.issubset(CreateSet2))
# CreateSet 与CreateSet2 先取差集,然后再从CreateSet中Remove掉

"""
True
False
"""

CreateSet2 是否是 CreateSet的子集,是子集就返回True

集合互斥–isdisjoint():判断A与B是否不相交

判断A集合与B集合是否没有相同的元素,如果没有相同的就返回True
“”" Return True if two sets have a null intersection. “”"

CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])

CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)

print(CreateSet.isdisjoint(CreateSet2))
print(CreateSet2.isdisjoint(CreateSet3))
# CreateSet 与CreateSet2 先取差集,然后再从CreateSet中Remove掉

"""
False
True
"""

你可能感兴趣的:(Python)