新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python空集合如何表示
python中,用set來(lái)表示一個(gè)無(wú)序不重復(fù)元素的序列。set的主要作用就是用來(lái)給數(shù)據(jù)去重。

可以使用大括號(hào) { } 或者 set() 函數(shù)創(chuàng)建集合,但是注意如果創(chuàng)建一個(gè)空集合必須用 set() 而不是 { },因?yàn)閧}是用來(lái)表示空字典類型的。
set的集合的創(chuàng)建與使用
#1.用{}創(chuàng)建set集合
person ={"student","teacher","babe",123,321,123} #同樣各種類型嵌套,可以賦值重復(fù)數(shù)據(jù),但是存儲(chǔ)會(huì)去重
print(len(person)) #存放了6個(gè)數(shù)據(jù),長(zhǎng)度顯示是5,存儲(chǔ)是自動(dòng)去重.
print(person) #但是顯示出來(lái)則是去重的
'''
5
{321, 'teacher', 'student', 'babe', 123}
'''
#空set集合用set()函數(shù)表示
person1 = set() #表示空set,不能用person1={}
print(len(person1))
print(person1)
'''
0
set()
'''
#3.用set()函數(shù)創(chuàng)建set集合
person2 = set(("hello","jerry",133,11,133,"jerru")) #只能傳入一個(gè)參數(shù),可以是list,tuple等 類型
print(len(person2))
print(person2)
'''
5
{133, 'jerry', 11, 'jerru', 'hello'}
'''更多學(xué)習(xí)內(nèi)容,請(qǐng)點(diǎn)擊python學(xué)習(xí)網(wǎng)。
文章名稱:創(chuàng)新互聯(lián)Python教程:python空集合如何表示
分享URL:http://www.fisionsoft.com.cn/article/dhodiec.html


咨詢
建站咨詢
