集合数据类型内置方法
一、集合
1.1 作用
存储一大堆元素的东西,容器数据类型
1.2 定义方式
在{}内用逗号分隔不可变数据类型
s = {1, 2, 1, 'a', 'a', 'c'}
print(s)
--------------------------------------------------------
{'c', 1, 2, 'a'} #去重:重复的全部去掉 #乱序:顺序是乱的
's = {}' #空的大括号是字典,而非集合,定义集合必须要用set()
1.3 内置方法
1.3.1优先掌握
1.长度
s = {1, 2, 1, 'a', 'a', 'c','4'}
print(f"len(s):{len(s)}")
-----------------------------------------------------
len(s):5
2.成员运算in和not in
s = {1, 2, 1, 'a', 'a', 'c','4'}
print(f"1 in s:{1 in s}")
print(f"1 not in s:{1 not in s}")
---------------------------------------------------------
name in dic:False
name not in dic:True
3.|并集union()
s = {1, 2, 1, 'a', 'a', 'c','4'}
s2 = {2,3,3,6,7,8}
print(s | s2)
s = {1,2,1, 'a', 'a', 'c','4'}
s2 = {2,3,3,6,7,8}
s = s.union(s2)
print(s)
------------------------------------------------------
{1, 2, 3, 6, 7, 8, 'c', '4', 'a'}
{1, 2, '4', 3, 6, 7, 8, 'c', 'a'}
4.&交集intersection()
s = {1, 2, 1, 'a', 'a', 'c','4'}
s2 = {2,3,3,6,7,8}
print(s & s2)
s = {1,2,1, 'a', 'a', 'c','4'}
s2 = {2,3,3,6,7,8}
s = s.intersection(s2)
print(s)
---------------------------------------------------------
{2}
{2}
5.-差集difference()
s = {1, 2, 1, 'a', 'a', 'c','4'}
s2 = {2,3,3,6,7,8}
print(s - s2)
s = {1,2,1, 'a', 'a', 'c','4'}
s2 = {2,3,3,6,7,8}
s = s.difference(s2)
print(s)
---------------------------------------------------------
{1, 'a', '4', 'c'}
{1, 'a', 'c', '4'}
6.^对称差集symmetric_difference
s = {1, 2, 1, 'a', 'a', 'c','4'}
s2 = {2,3,3,6,7,8}
print(s ^ s2)
s = {1,2,1, 'a', 'a', 'c','4'}
s2 = {2,3,3,6,7,8}
s = s.symmetric_difference(s2)
print(s)
-----------------------------------------------------------
{1, 3, 6, 7, 8, '4', 'a', 'c'}
{1, 3, 6, 7, 8, '4', 'a', 'c'}
1.3.了解
1.copy()
s = {1, 2, 1, 'a', 'a', 'c','4'}
s.copy()
print(s)
----------------------------------------------------
{'a', 1, 2, '4', 'c'}
2.update()
s = {1, 2, 1, 'a', 'a', 'c','4'}
s2 = {2,3,3,6,7,8}
s.update(s2)
print(s)
{1, 2, 3, 6, 7, 8, 'c', 'a', '4'}
3.clear()
s = {1, 2, 1, 'a', 'a', 'c','4'}
s.clear()
print(s)
---------------------------------------------------------
set()
4.pop()
s = {1, 2, 1, 'a', 'a', 'c','4'}
s.pop() #随机删除
print(s)
---------------------------------------------------------
{2, 'c', 'a', '4'}
5.remove() :删除
#如果在集合中找不到要删除的元素则会报错
s = {1, 2, 1, 'a', 'a', 'c','4'}
s.remove('a')
print(s)
-----------------------------------------------------------
{1, 2, '4', 'c'}
6.discard():删除
#如果在集合中找不到要删除的元素则不会会报错
s = {1, 2, 1, 'a', 'a', 'c','4'}
s.discard('a')
print(s)
----------------------------------------------------------
{1, 2, 'c', '4'}
7.add()
s = {1, 2, 1, 'a', 'a', 'c','4'}
s.add(9)
print(s)
---------------------------------------------------------
{1, 2, 9, 'a', 'c', '4'}
8.difference_update()
s = {1,2,1, 'a', 'a', 'c','4'}
s2 = {2,3,3,6,7,8}
s.difference_update(s2)
print(s)
----------------------------------------------------------
{'a', 1, '4', 'c'}
9.子集issubset()
s = {1,2,1, 'a', 'a', 'c','4'}
s2 = {2,3,3,6,7,8}
s = s.issubset(s2)
print(s)
----------------------------------------------------------
False
10.父集
s = {1,2,3, 'a', 'a', 'c','4'}
s2 = {2,3}
s = s.issuperset(s2)
print(s)
---------------------------------------------------------
True
1.4 有一个值还是多个值
多个值
1.5 有序(索引)or无序(索引)
无序
1.6 可变or不可变
可变
s = {1,2}
print(id(s))
s.add(3)
print(id(s))
-------------------------------------------------------------
2199963714952
2199963714952