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)文章
python3.4用函數(shù)操作mysql5.7數(shù)據(jù)庫
這篇文章主要為大家詳細介紹了python3.4用函數(shù)操作mysql5.7數(shù)據(jù)庫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
解決tensorflow由于未初始化變量而導(dǎo)致的錯誤問題
今天小編就為大家分享一篇解決tensorflow由于未初始化變量而導(dǎo)致的錯誤問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
如何利用python實現(xiàn)圖片轉(zhuǎn)化字符畫
這篇文章主要介紹了如何利用python實現(xiàn)圖片轉(zhuǎn)化字符畫,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06
python數(shù)字轉(zhuǎn)對應(yīng)中文的方法總結(jié)
在本篇文章里小編給大家分享的是一篇關(guān)于python數(shù)字轉(zhuǎn)對應(yīng)中文的方法總結(jié)內(nèi)容,有興趣的朋友們可以跟著猜嘗試測試下。2021-08-08
Python+OpenCV實現(xiàn)邊緣檢測與角點檢測詳解
這篇文章主要為大家詳細介紹了如何通過Python+OpenCV實現(xiàn)邊緣檢測與角點檢測,文中的示例代碼講解詳細,對我們學(xué)習(xí)Python與OpenCV有一定的幫助,需要的可以參考一下2023-02-02
淺談django的render函數(shù)的參數(shù)問題
今天小編就為大家分享一篇淺談django的render函數(shù)的參數(shù)問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python常用類型轉(zhuǎn)換實現(xiàn)代碼實例
這篇文章主要介紹了Python常用類型轉(zhuǎn)換實現(xiàn)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07

