網站首頁 編程語言 正文
前言:
集合這種數據類型和我們數學中所學的集合很是相似,數學中堆積和的操作也有交集,并集和差集操作,python集合也是一樣。
一、交集操作
1.使用intersection()求交集
可變集合和不可變集合求交集的時候,用什么集合調用交集方法,返回的結果就是什么類型的集合。
set7 = {'name', 18, 'python2', 'abc'} set8 = frozenset({'name', 19, 'python3', 'abc'}) res = set7.intersection(set8) ?# {'abc', 'name'}print(res, type(res)) res = set8.intersection(set7) ?# frozenset({'abc', 'name'}) print(res, type(res))
返回結果:
{'abc', 'name'}
frozenset({'abc', 'name'})
2. 使用位運算&符求交集
set5 = {'name', 18, 'python2', 'abc'} set6 = {'name', 19, 'python3', 'abc'} set7 = {'name', 18, 'python2', 'abc'} set8 = frozenset({'name', 19, 'python3', 'abc'}) res = set5 & set6 print(res, type(res)) res = set7 & set8 print(res, type(res)) res = set8 & set7 ?# 誰在前,返回結果就和誰是同樣類型的集合 print(res, type(res))
返回結果:
{'abc', 'name'}
{'abc', 'name'}
frozenset({'abc', 'name'})
3.intersection_update()方法
使用此方法計算出交集之后會把結果賦值給原有的集合,屬于一種更改,所以不適用于不可變集合
set7 = {'name', 18, 'python2', 'abc'} set8 = frozenset({'name', 19, 'python3', 'abc'}) res = set7.intersection_update(set8) ?# 沒有返回值 print(set7, type(set7)) ?# 沒有返回值,直接打印被賦值集合 res = set8.intersection_update(set7) ?# 不可變集合沒有intersection_update方法 print(res, type(res))
返回結果:
{'abc', 'name'}
AttributeError: 'frozenset' object has no attribute 'intersection_update'
4.使用intersection()方法
使用此方法求集合和其他數據類型的交集時intersection()
會把其他數據類型直接轉為集合。
str1 = 'python' list1 = [1, 2, 3, 18] tup1 = (1, 2, 3, 18) dict1 = {'name': 'Tom', 'age': 18, 'love': 'python'} set10 = {'name', 18, 'python', 'abc', 'p'} print(set10.intersection(str1)) ? # 返回:{'p'}而不是{'python'},因為str1轉成集合為:{'y', 't', 'p', 'o', 'n', 'h'} ? print(set10.intersection(list1)) print(set10.intersection(tup1)) print(set10.intersection(dict1))
返回結果:
{'p'}
{18}
{18}
{'name'}
二、并集操作
1.使用union()求并集
set5 = {'name', 18, 'python2', 'abc'} set6 = {'name', 19, 'python3', 'abc'} res = set5.union(set6) print(res, type(res))
返回結果:
{'python2', 'abc', 18, 19, 'python3', 'name'}
2.使用邏輯或 | 求并集
set5 = {'name', 18, 'python2', 'abc'} set6 = {'name', 19, 'python3', 'abc'} res = set5 | set6 print(res, type(res))
返回結果:
{'abc', 'python2', 'name', 'python3', 18, 19}
3.使用update()求并集,只能作用域可變集合
set5 = {'name', 18, 'python2', 'abc'} set6 = {'name', 19, 'python3', 'abc'} res = set5.update(set6) ?# 有黃色波浪線表示這個函數沒有返回值 print(set5, type(set5))
返回結果:
{'python2', 'python3', 18, 'abc', 19, 'name'}
原文鏈接:https://blog.csdn.net/weixin_48728769/article/details/121718480
相關推薦
- 2022-05-31 關于k8s?使用?Service?控制器對外暴露服務的問題_云其它
- 2023-01-07 Python中層次聚類的詳細講解_python
- 2022-05-25 Docker?compose配置文件寫法及命令使用示例_docker
- 2022-07-12 strcpy、strncpy與memcpy的區別你了解嗎?
- 2022-11-24 Python文件簡單操作及openpyxl操作excel文件詳解_python
- 2022-08-21 在WPF中實現全局快捷鍵功能_C#教程
- 2022-09-10 nginx?Rewrite重寫地址的實現_nginx
- 2022-08-25 windows下搭建Consul集群_云其它
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支