Python中集合的創(chuàng)建及常用函數(shù)的使用詳解
集合的創(chuàng)建
使用內(nèi)置函數(shù)set()進行轉(zhuǎn)化或者使用{}包括起來的,集合中的元素:無序性,互異性,確定性。
舉例:
numbers=set(range(0,7))//使用內(nèi)置函數(shù)進行轉(zhuǎn)化 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])
集合中的操作函數(shù)
在集合中添加元素
add() 函數(shù)
舉例:
fruit =set({'apple',9,"axn","dbu",12}) fruit.add("bc") print(fruit)
輸出:
{'bc', 'apple', 9, 12, 'dbu', 'axn'}
刪除集合中的第一個元素
pop()函數(shù)
舉例:
fruit =set({'apple',9,"axn","dbu",12}) fruit.pop() print(fruit)
輸出:
{'apple', 9, 12, 'axn'}
刪除集合中的指定元素
1:remove()函數(shù),若該元素不存在則會報錯
舉例:
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()函數(shù),若指定元素不存在不會報錯
舉例:
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循環(huán)
fruit =set({'apple',9,"axn","dbu",12,"apple"}) for i in fruit: print(i,end=' ')
輸出:
axn 9 apple 12 dbu
集合元素個數(shù)的計算
len()函數(shù)
舉例:
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'>
使用內(nèi)置函數(shù)進行轉(zhuǎn)化:
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'>
到此這篇關(guān)于Python中集合的創(chuàng)建及常用函數(shù)的使用詳解的文章就介紹到這了,更多相關(guān)Python集合內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實現(xiàn)發(fā)送和獲取手機短信驗證碼
這篇文章主要介紹了python實現(xiàn)發(fā)送和獲取手機短信驗證碼的相關(guān)資料,講解了python如何解決接口測試獲取手機驗證碼問題,感興趣的小伙伴們可以參考一下2016-01-01python和websocket構(gòu)建實時日志跟蹤器的步驟
這篇文章主要介紹了python和websocket構(gòu)建實時日志跟蹤器的步驟,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下2021-04-04使用PyTorch將數(shù)據(jù)從CPU移動到GPU的四個方法
這篇文章給大家介紹了在 PyTorch 中,將數(shù)據(jù)從 CPU 移動到 GPU 的幾種方法,使用 .to() 方法,使用 .cuda() 方法,使用 torch.Tensor 構(gòu)造函數(shù)和使用 torch.tensor 構(gòu)造函數(shù)這四個方法,通過代碼示例介紹非常詳細,需要的朋友可以參考下2024-01-01Python?SQLAlchemy與數(shù)據(jù)庫交互操作完整指南
SQLAlchemy 是一個強大的 Python 庫,用于數(shù)據(jù)庫操作,無論是簡單的數(shù)據(jù)存儲還是復雜的數(shù)據(jù)管理,SQLAlchemy 都提供了多種方法來處理數(shù)據(jù)庫,本文將全面介紹 SQLAlchemy的基本用法以及各種操作的示例代碼2024-01-01