python中的集合类型(全家福)

简介: python中的集合类型(全家福)

白话学python,就找三岁学编程,用最简谱的话带你从0开始


python集合是什么?



python中也有集合类型(set

该集合类型与数学的集合基本相似。

概念:包含0个或多个数据项的无序组合。集合元素不可重复。

集合中只包含整数、浮点型、字符串、元组等不可变数据。


无序组合


由于集合的无序性所以没有索引和切片的概念

不能够切片

可以动态添加或删除元素。


元素不可重复


集合里面的元素是单独的唯一的,不可以重复的

使用集合可以把其他数据类型的内容过滤重复元素


set() 函数


set()函数把其他类型的转换成集合。


#集合的创建及无序性
>>> s = {123, 'python', (13, 14), 5.18}
>>> s
{(13, 14), 123, 5.18, 'python'}
>>> t = {123, 'python', (13, 14), 5.18, 'hallo'}
>>> t
{5.18, 'python', (13, 14), 'hallo', 123}
#set() 函数的使用
>>> w = set('python')
>>> w
{'n', 'y', 'h', 'p', 't', 'o'}
>>> v = set(((13, 14), 123, 5.18, 'python'))
>>> v
{(13, 14), 123, 5.18, 'python'}
#不可重复性
>>> c = {3, 5, 6, 3}
>>> c
{3, 5, 6}
>>> a = 3, 6, 9, 6 #创建元组a
>>> a = set(a)# 将元组转换成列表
>>> a
{9, 3, 6}


集合类型的操作符


集合共有4种基本操作:


交集 (&)
并集 (|)
差集 (-)
补集 (^)


集合类型操作符


image.png


差集


20200404140208697.jpg


集合A和B的差集就是两者的差


S - T or S.difference(T)


返回一个新的集合,包括在集合S但是不包括在T中的元素


>>> s - t
set()
>>> t-s
{'hallo'}
>>> s.difference(t)
set()
>>> t.difference(s)
{'hallo'}


S -= T or S.difference_update(T)


更新集合S ,返回S中有的但是T中没有的元素


>>> s -= t
>>> s
set()
>>> s = {123, 'python', (13, 14), 5.18}
>>> t = {123, 'python', (13, 14), 5.18, 'hallo'}
>>> t -= s
>>> t
{'hallo'}
>>> s = {123, 'python', (13, 14), 5.18}
>>> t = {123, 'python', (13, 14), 5.18, 'hallo'}
>>> t.difference_update(s)
>>> t
{'hallo'}
>>> s = {123, 'python', (13, 14), 5.18}
>>> t = {123, 'python', (13, 14), 5.18, 'hallo'}
>>> s.difference_update(t)
>>> s
set()


交集


A和B相同的部分


20200404141053995.jpg


S&T or S.intersection(T)


返回一个新集合,包括在S和T中的相同的元素

>>> s = {123, 'python', (13, 14), 5.18}
>>> t = {123, 'python', (13, 14), 5.18, 'hallo'}
>>> s & t
{(13, 14), 123, 5.18, 'python'}
>>> t & s
{(13, 14), 123, 5.18, 'python'}
>>> s.intersection(t)
{(13, 14), 123, 5.18, 'python'}
>>> t.intersection(s)
{(13, 14), 123, 5.18, 'python'}


这里s和t的顺序无关,结果都一样


S&=T or S.intersection_update(T)


更新集合S,返回集合S和T中共有的元素


>>> s &= t
>>> s
{(13, 14), 123, 5.18, 'python'}
>>> t &= s
>>> t
{(13, 14), 123, 5.18, 'python'}
>>> s.intersection_update(t)
>>> s
{(13, 14), 123, 5.18, 'python'}
>>> t.intersection_update(s)
>>> t
{(13, 14), 123, 5.18, 'python'}


这里要注意s和t的位置,结果是不一样的


并集


A和B的并集是A和B里面所有的元素


20200404151158626.jpg


S|T or S.union(T)


返回一个新的集合,包括S和T中所有的元素


>>> s = {(13, 14), 123, 5.18, 'python'}
>>> t = {5.18, 'python', (13, 14), 'hallo', 123}
>>> s | t
{5.18, 'python', (13, 14), 'hallo', 123}
>>> s.union(t)
{5.18, 'python', (13, 14), 'hallo', 123}


因为不改变原有代码,s和 t 的顺序不会产生影响


S|=T or S.updat(T)


更新集合S,返回 S 和 T 的所有元素


>>> s |= t
>>> s
{5.18, 'python', (13, 14), 'hallo', 123}
>>> t |= s
>>> t
{5.18, 'python', (13, 14), 'hallo', 123}
>>> s = {(13, 14), 123, 5.18, 'python'}
>>> t.update(s)
>>> t
{5.18, 'python', (13, 14), 'hallo', 123}
>>> s.update(t)
>>> s
{5.18, 'python', (13, 14), 'hallo', 123}


因为会更新原有集合,所以注意顺序


补集


A 和 B 集合中不相同的元素


20200404153628472.jpg


S^T or S.symmetric_difference(T)


返回一个新集合,包含 S 和T 之中所有不同的元素

>>> s = {(13, 14), 123, 5.18, 'python'}
>>> s ^ t
{'hallo'}
>>> s.symmetric_difference(t)
{'hallo'}


返回新集合,s 和 t 的位置无关


S^=T or S.symmetric_difference_update(T)


更新集合 S ,返回集合 S 和 T 集合的不相同值

>>> s ^= t
{'hallo'}
>>> t ^= s
{5.18, 'python', (13, 14), 123}
>>>s.symmetric_difference_update(t)
>>> s
{'hallo'}
>>> t.symmetric_difference_update(s)
>>> t
{'hallo'}


更新了数组,注意顺序和位置


S和T数组比较


S<=T or S.issubset(T)


S 与 T相同,或是其子集则返回 True 否则返回 False

用 S<T 判定S 是不是T 的真子集


>>> s = {(13, 14), 123, 5.18, 'python'}
>>> t={5.18, 'python', (13, 14), 'hallo', 123}
>>> s <= t
True
>>> s.issubset (t)
True
>>> t <= s
False
>>> t.issubset(s)
False


S>=T or S.issuperset(T)


与上面的相反,S 与 T 相同或 S 是 T 的子集,返回 True ,否者返回 False

用 S>T 来判断 S 是不是 T 的 真超集

>>> s >= t
False
>>> s.issuperset(t)
False
>>> t >= s
True
>>> t.issuperset(s)
True


集合类型的操作函数或方法


image.png


S.add(X)


如果 元素 X 不在 S 集合中则添加到集合 S

>>> s.add(250)
>>> s
{5.18, 'python', (13, 14), 250, 123}


S.clear()


移除 S 中的所有数据项

>>> s = {5.18, 'python', (13, 14), 250, 123}
>>> s.clear()
>>> s
set()


注:移除数据项,不是删除整个集合


S.copy()


返回一个集合的副本

注:这里是副本,不是返回集合


>>> s.copy()
{(13, 14), 123, 5.18, 'python'}


S.pop()


随机返回集合的一个元素,若S为空则报错!

>>> s.pop()
(13, 14)
>>> s.pop()
123
#报错测试
>>> b = set()
>>> b.pop()
Traceback (most recent call last):
  File "<pyshell#94>", line 1, in <module>
    b.pop()
KeyError: 'pop from an empty set'


S.discard(X) or S.remove(X)


S.discard(X) | 如果元素 X 在集合 S 中,则删除,不在也不报错

S.remove(X) | 如果元素 X 在集合 S 中,则删除,不在则报错(keyError异常)


# S.discard(X)
>>> s = {5.18, 'python', (13, 14), 250, 123}
>>> s.discard(123)
>>> s
{5.18, 'python', (13, 14), 250}
>>> s.discard(6)
>>> s
{5.18, 'python', (13, 14), 250}
#S.remove(X)
>>> s = {5.18, 'python', (13, 14), 250, 123}
>>> s.remove(123)
>>> s
{5.18, 'python', (13, 14), 250}
>>> s.remove(12)
Traceback (most recent call last):
  File "<pyshell#105>", line 1, in <module>
    s.remove(12)
KeyError: 12


S.isdisjoint(T)


若 S 和 T 没有相同的元素则返回True

>>> s.isdisjoint(t)
False


X in S or X not in S


X in S | 如果X 是S的元素返回 True 否则返回False

X not in S | 如果X 不是S的元素返回 True 否则返回False


>>> 'python' in s
True
>>> 'python' not in s
False
>>> 'hallo' in s
False
>>> 'hallo' not in s
True


优势


集合的单一性,不包括重复的元素是特色

所以有需要数据去重的可以用集合来完成


冰冻的集合


冰冻的被冻住了那么久无法修改了,这是一个特殊的集合又叫不可变集合


frozenset()


定义冰冻的集合(不可变集合)


>>> a = frozenset('pythopn')
>>> a
frozenset({'y', 'p', 'h', 't', 'n', 'o'})


既然是不可变的集合那么上面的

添加、删除就不存在了


没有add()、remove()、clear()、pop()等函数


>>> a.add('c') #add()报错
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    a.add('c')
AttributeError: 'frozenset' object has no attribute 'add'
>>> a.clear('h')#clear() 报错
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    a.clear('h')
AttributeError: 'frozenset' object has no attribute 'clear'
>>> a.remove('h')#remove()报错
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    a.remove('h')
AttributeError: 'frozenset' object has no attribute 'remove'
>>> a.pop()# pop()报错
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    a.pop()
AttributeError: 'frozenset' object has no attribute 'pop'
>>> a.discard('h')#discard()报错
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    a.discard('h')
AttributeError: 'frozenset' object has no attribute 'discard'


以上函数均不适用于冰冻集合,估计是他太高冷,没有那么平易近人。

他有哈希值,可以作为字典的键,也可以作为元素,就是不能够修改,比较硬核。

集合的整理就告一段落了,有问题记得留言,好的记得收藏


点个赞,给小编一点,鼓励,整理不易啊

谢谢大家!!!

目录
相关文章
|
4天前
|
Python
【Python操作基础】——集合
【Python操作基础】——集合
|
4天前
|
Python
在Python中,全局变量和局部变量是两种不同类型的变量
【5月更文挑战第10天】Python中的全局变量在函数外部定义,作用域覆盖整个程序,生命周期从开始到结束。局部变量仅限于函数内部,生命周期从调用到返回。在函数内修改全局变量需用`global`关键字声明,否则会创建局部变量。
21 3
|
2天前
|
存储 索引 Python
【python学习】列表、元组、字典、集合,秋招是不是得到处面试
【python学习】列表、元组、字典、集合,秋招是不是得到处面试
|
4天前
|
IDE 开发工具 Python
Python类型检查
【5月更文挑战第9天】Python类型检查
10 1
|
4天前
|
Python
Python使用类型注解
【5月更文挑战第7天】
8 2
|
4天前
|
JSON 数据格式 Python
Python 的 requests 库是一个强大的 HTTP 客户端库,用于发送各种类型的 HTTP 请求
【5月更文挑战第9天】`requests` 库是 Python 中用于HTTP请求的强大工具。要开始使用,需通过 `pip install requests` 进行安装。发送GET请求可使用 `requests.get(url)`,而POST请求则需结合 `json.dumps(data)` 以JSON格式发送数据。PUT和DELETE请求类似,分别调用 `requests.put()` 和 `requests.delete()`。
31 2
|
4天前
|
Java C# 开发者
Python 中的类型注解是一种用于描述变量、函数参数和返回值预期类型的机制
【5月更文挑战第8天】Python的类型注解提升代码可读性和可维护性,虽非强制,但利于静态类型检查(如Mypy)。包括:变量注解、函数参数和返回值注解,使用内置或`typing`模块的复杂类型,自定义类型注解,以及泛型模拟。类型注解可在变量声明、函数定义和注释中使用,帮助避免类型错误,提高开发效率。
23 6
|
4天前
|
数据处理 Python
Python中的数字类型
【5月更文挑战第6天】Python数字类型:无限制大小的整型(int,包括二、八、十、十六进制),浮点型(float,支持科学记数法),布尔型(bool,True/False),和复数型(complex,如a+bi)。用于数值计算和数据处理。
18 0
|
4天前
|
索引 Python
python类型错误(TypeError)
【5月更文挑战第1天】
63 2
|
4天前
|
Python
Python中字典和集合(二)
Python中字典和集合(二)