Python基础_集合基本操作

2022-10-10 08:49:57

Python集合_基本操作

集合的2个重要特点:
A、元素都是不重复的,唯一的,因此可以用来去重;
B、集合是无序的,不支持通过索引访问元素,不支持切片。
集合的分类:
A、可变集合set;
B、不可变集合frozenset:是不能被改变的,即不能插入和删除,类似于元组。


一、创建空集合

a = set()
type(a)   # <class 'set'>
b = set([])
type(b)   # <class 'set'>
c = set({})
type(c)   # <class 'set'>
d = set(())
type(d)   # <class 'set'>
a == b  # True
b == c  # True
c == d  # True

二、增
1) add: 元素作为一个整体添加

a= set ()
a.add(1)
a.add(2)
print(a)   #返回{1,2}
a.add(1)
print(a)   #返回{1,2},因为集合元素的唯一性,添加相同元素,原集合没有变化
a.add("xyz")
print(a)  #返回 {1,2,"xyz"}

2) update: 将传入的字符串元素,拆成单个字符新增到集合

se = {"a","b"}
se.update("cde")
print(se)  # 返回{"a","b","c","d","e"},将传入的字符串元素,拆成单个字符新增到集合

三、删
1)remove(): 删除集合中的单个指定元素

se = {'a', 'e', 'c', 'b', 'd'}
se.remove("a")
print(se)  # 返回 {'e', 'c', 'b', 'd'}
# 删除不存在的元素报错
se.remove("x")   # 报错KeyError: 'x'

2)discard(元素): 查找并删除指定元素
函数功能:
在集合中查找元素,如果找到就将其删除;如果没找到则什么也不做。该函数没有返回值

se = {'a', 'e', 'c', 'b', 'd'}
se.discard("a")
print(se)  # 返回 {'e', 'c', 'b', 'd'}
# 删除不存在的元素,不会报错
se.discard("x")

3) pop(): 删除集合中的第一个元素
集合是无序的,这里说第一个元素,并不是很准确,应该是弹出最左边的元素。

se = {'a', 'e', 'c', 'b', 'd'}
# 循环执行5次,每次都是弹出第一个元素
for  i  in range(5):
    se.pop()

se =set()   #空集合
se.pop()  #  报错KeyError: 'pop from an empty set'

4) clear() : 清空集合中的元素

se = {'a', 'e', 'c', 'b', 'd'}
se.clear()
print(se)  # 返回空集合

5)del : 从内存中清除

se = {'a', 'e', 'c', 'b', 'd'}
del se
print(se)  #报错 NameError: name 'se' is not defined

四、查
因为集合是无序的,因此不支持按索引位置访问,也不支持切片查找,也不支持按索引位置遍历。
1)因此,只支持按元素的值遍历

se = {'a', 'e', 'c', 'b', 'd'}
for i in se:
    print(i)

2)非要打印当前集合元素的顺序,要借助enumerate函数。

se = {'a', 'e', 'c', 'b', 'd'}
for index,i in enumerate(se):print(index,i)

五、交集
1)符号: &
语法: 集合1&集合2

a=set("abcd")
b=set("cdef")
a&b
{'c', 'd'}

2) intersection()
语法: 集合1.intersection(集合2)

s1 = {1,2,3,4}
s2 = {3,4,5,6}
s1.intersection(s2)
{3, 4}

3) intersection_update()
语法: 集合1.intersection_update(集合2)
效果: 取到集合1与集合2的交集,然后将集合1更新为得到的交集

s1={1,2,3,4}
s2={3,4,5,6}
s1.intersection_update(s2)
s1  # S1已经被更新,返回 {3, 4}
s2  # S2不变,返回 {3,4,5,6}

六、并集 :两个集合所有的并去掉重复的元素组成的集合
1) 符号: |

a=set("abcd")
b=set("cdef")
a|b  # 返回{'a', 'd', 'c', 'b', 'e', 'f'}

2) union()
语法: 集合1.union(集合2)

s1={1,2,3,4}
s2={3,4,5,6}
s1.union(s2)  # 返回{1, 2, 3, 4, 5, 6}

3)update()
语法: 集合1.update(集合2)
效果: 取到集合1与集合2的并集,然后将集合1更新为得到的并集

s1={1,2,3,4}
s2={3,4,5,6}
s1.update(s2)
s1   # s1被更新为并集 {1, 2, 3, 4, 5, 6}
s2   # s2不变 {3, 4, 5, 6}

七、差集 :两个集合所有的并去掉重复的元素组成的集合
1)符号 -

a=set("abcd")
b=set("cdef")
a-b   # 返回  {'a', 'b'}
b-a   # 返回  {'e', 'f'}

2) difference()
语法: 集合1.difference(集合2)

s1={1,2,3,4}
s2={3,4,5,6}
s1.difference(s2)   # 返回 {1, 2}

七、对称差集 :symmetric_difference(),两个集合中,互不属于对方集合的元素,的并集,即 (A-B) | (B-A) 。
语法: 集合1.symmetric_difference(集合2)

s1 = {1,2,3,4}
s2 = {3,4,5,6}
s1.symmetric_difference(s2)   #返回 {1, 2, 5, 6}

八、^
功能: 两个集合中除去相同集合的元素组成的集合
即 (A|B) - (A&B) ,也就是两个集合的(并集与交集)的差集

a=set("abcd")
b=set("cdef")
a^b  # 返回{'a', 'b', 'e', 'f'}
  • 作者:小菜鸡也会有春天
  • 原文链接:https://blog.csdn.net/weixin_50640351/article/details/115472523
    更新时间:2022-10-10 08:49:57