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

可以使用大括號 { } 或者 set() 函數(shù)創(chuàng)建集合,但是注意如果創(chuàng)建一個空集合必須用 set() 而不是 { },因為{}是用來表示空字典類型的。
set的集合的創(chuàng)建與使用
#1.用{}創(chuàng)建set集合
person ={"student","teacher","babe",123,321,123} #同樣各種類型嵌套,可以賦值重復(fù)數(shù)據(jù),但是存儲會去重
print(len(person)) #存放了6個數(shù)據(jù),長度顯示是5,存儲是自動去重.
print(person) #但是顯示出來則是去重的
'''
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")) #只能傳入一個參數(shù),可以是list,tuple等 類型
print(len(person2))
print(person2)
'''
5
{133, 'jerry', 11, 'jerru', 'hello'}
'''更多學(xué)習(xí)內(nèi)容,請點擊python學(xué)習(xí)網(wǎng)。
當(dāng)前名稱:創(chuàng)新互聯(lián)Python教程:python空集合如何表示
網(wǎng)站地址:http://www.dlmjj.cn/article/dhodiec.html


咨詢
建站咨詢
