網站首頁 編程語言 正文
集合的創建
使用內置函數set()進行轉化或者使用{}包括起來的,集合中的元素:無序性,互異性,確定性。
舉例:
numbers=set(range(0,7))//使用內置函數進行轉化
print(numbers)
print(type(numbers))
輸出:
{0, 1, 2, 3, 4, 5, 6}
<class 'set'>
互異性
fruit={'apple','orange','banana',"apple",'apple','orange','banana',"apple"}
print(fruit)
print(type(fruit))
輸出:
{'apple', 'banana', 'orange'}
<class 'set'>
無序性
集合中的元素不能通過下標訪問。
舉例:
fruit =set({'apple',9,"axn","dbu",12})
print(fruit[2])
集合中的操作函數
在集合中添加元素
add() 函數
舉例:
fruit =set({'apple',9,"axn","dbu",12})
fruit.add("bc")
print(fruit)
輸出:
{'bc', 'apple', 9, 12, 'dbu', 'axn'}
刪除集合中的第一個元素
pop()函數
舉例:
fruit =set({'apple',9,"axn","dbu",12})
fruit.pop()
print(fruit)
輸出:
{'apple', 9, 12, 'axn'}
刪除集合中的指定元素
1:remove()函數,若該元素不存在則會報錯
舉例:
fruit =set({'apple',9,"axn","dbu",12})
fruit.remove("banana")
print(fruit)
fruit =set({'apple',9,"axn","dbu",12,"apple"})
fruit.remove("apple")
print(fruit)
輸出:
{'dbu', 'axn', 9, 12}
2:discard()函數,若指定元素不存在不會報錯
舉例:
fruit =set({'apple',9,"axn","dbu",12,"apple"})
fruit.discard("banana")
print(fruit)
輸出:
{'dbu', 'apple', 9, 'axn', 12}
fruit =set({'apple',9,"axn","dbu",12,"apple"})
fruit.discard("apple")
print(fruit)
輸出:
{'dbu', 'axn', 9, 12}
判斷元素是否在集合里面
if in/not in語句
舉例:
fruit =set({'apple',9,"axn","dbu",12,"apple"})
if "apple" in fruit:
print("yes")
else:
print("NO")
if "banana" not in fruit:
print("YES")
else:
print("NO")
輸出:
yes
YES
集合的遍歷
for循環
fruit =set({'apple',9,"axn","dbu",12,"apple"})
for i in fruit:
print(i,end=' ')
輸出:
axn 9 apple 12 dbu?
集合元素個數的計算
len()函數
舉例:
fruit =set({'apple',9,"axn","dbu",12,"apple"})
print(len(fruit))
輸出:
5//注意集合元素的唯一性特征
集合與字典,列表,元組的嵌套
集合與字典:
s1=set({"name":"jason","age":19,"地址":"北京市"})
print(s1)
print(type(s1))
輸出:
{'地址', 'name', 'age'}//只輸出鍵名
<class 'set'>
集合與元組
舉例:
s1={("name","jason","age",19,"地址","北京市"),12,34,0}
print(s1)
print(type(s1))
輸出:
{0, 34, ('name', 'jason', 'age', 19, '地址', '北京市'), 12}
<class 'set'>
使用內置函數進行轉化:
s1=set({"name","jason","age",19,"地址","北京市"})
print(s1)
print(type(s1))
輸出:
{'age', 'jason', 19, '地址', '北京市', 'name'}
<class 'set'>
集合與列表
舉例:
s2=set(["name","jason","age",19,"地址","北京市"])
print(s2)
print(type(s2))
輸出:
{'北京市', 'age', 'jason', 19, 'name', '地址'}
<class 'set'>
原文鏈接:https://blog.csdn.net/m0_64365419/article/details/125420103
相關推薦
- 2022-04-03 Go語言讀取txt文檔的操作方法_Golang
- 2022-12-30 antd之RangePicker設置默認值方式_React
- 2023-07-02 Python利用scikit-learn實現近鄰算法分類的示例詳解_python
- 2022-08-23 Selenium多窗口切換解決方案_python
- 2022-05-02 C語言實現簡單回聲服務器_C 語言
- 2022-02-11 Android之Compose頁面切換動畫介紹_Android
- 2022-08-20 Linux中sftp常用命令整理_linux shell
- 2022-02-14 記關于Android開發中使用System.currentTimeMillis()不準確的問題
- 最近更新
-
- 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同步修改后的遠程分支