Python-34 - 陣列介紹 - set() 使用說明

set() 使用說明

set() 三大特點

  1. 無序號
  2. 無索引(隨機出現)
  3. 沒有重覆(一樣的只會出現一次)

一筆筆的讀資料 : for 迴圈 

抓出set()總共有幾筆資料:len()

現有陣列上添加一筆資料:add()

現有的陣列上添加多筆資料:update()

查詢陣列中有沒有我要的資料: in (只回傳true 或 false)

刪除資料元素 discard() or remove()

清空陣列 clear()、del


 

set() 使用說明

 兩種方式都可以表示set() 資料

it3c= {"computer","notebook","phone"}
print(it3c)


myclass = set(("java","python","rwd","ps","dw"))
print(myclass)

看看效果

#無序號

#無序號
it3c= {"computer","notebook","phone"}
print(it3c[1])
"""
會跳錯誤訊息

python set_1.py 
python: can't open file 'set_1.py': [Errno 2] No such file or directory
"""

#無索引,隨機顯示資料


#隨機顯示資料
myclass = set(("java","python","rwd","ps","dw"))
print(myclass)

#不會重複


#資料不能重複(重複時只會印出一個)
myclass = set(("java","java","python","rwd","ps","dw"))
print(myclass)

看看效果

 

使用for迴圈一筆筆的讀資料
it3c = {"computer","notebook","phone"}

for a in it3c: 
    print(a)

print("=========")   

myclass = set(("java","python","rwd","ps","ai","dw"))
for b in myclass:
    print (b)

看看效果

使用len()函數抓出set()總共有幾筆資料
#查詢資料共有幾筆
it3c = {"computer","notebook","phone"}

print(len(it3c))  #3筆

myclass = set(("java","python","rwd","ps","ai","dw"))

total =len(myclass)

print(total) #6筆

看看效果

現有的陣列上添加一筆資料
#現有的陣列上添加一筆資料
it3c = {"computer","notebook","phone"}
it3c.add("laptop")
print(it3c) #{'laptop', 'computer', 'notebook', 'phone'}

myclass = set(("java","python","rwd","ps","ai","dw"))
myclass.add("js")
print(myclass) #{'dw', 'python', 'rwd', 'java', 'ai', 'ps', 'js'}
 

看看效果

現有的陣列上添加多筆資料
#現有的陣列上添加多筆資料
it3c = {"computer","notebook","phone"}
it3c.update(["aaa","bbb","ccc"])
print(it3c)
#{'computer', 'bbb', 'phone', 'notebook', 'ccc', 'aaa'}

myclass = set(("java","python","rwd","ps","ai","dw"))
myclass.update(["123","456","789"])
print(myclass)
#{'java', 'dw', 'ps', 'ai', '123', '456', 'rwd', 'python', '789'}

看看效果

查詢陣列中有沒有我要的資料
it3c = {"computer","notebook","phone"}

print ("phoneX" in it3c) #False



myclass = set(("java","python","rwd","ps","ai","dw"))

aa=("python" in myclass)

print(aa) #True

皆可以使用在 set()、list()、tuple()上

#查詢陣列中有沒有我要的資料-set
it3c = {"computer","notebook","phone"}
print ("phoneX" in it3c) #False

myclass = set(("java","python","rwd","ps","ai","dw"))
aa=("python" in myclass) 
print(aa) #True


#查詢陣列中有沒有我要的資料 -list類型
it3c = ["computer","notebook","phone"]
print ("phoneX" in it3c) #False

myclass = list(("java","python","rwd","ps","ai","dw"))
aa=("python" in myclass) 
print(aa) #True

#查詢陣列中有沒有我要的資料 -tuple類型
it3c = {"computer","notebook","phone"}
print ("phoneX" in it3c) #False

myclass = tuple(("java","python","rwd","ps","ai","dw"))
aa=("python" in myclass) 
print(aa) #True

看看效果

刪除資料元素 discard() or remove()

it3c = {"computer","notebook","phone"}

print(it3c) #{'computer', 'phone', 'notebook'}

it3c.discard("phone")

print(it3c)#{'notebook', 'computer'}


myclass = set(("java","python","rwd","ps","ai","dw"))

print(myclass)#{'ai', 'python', 'ps', 'dw', 'rwd', 'java'}

myclass.remove("rwd")

print(myclass)#{'ai', 'python', 'ps', 'dw', 'java'}

看看效果

清空陣列 clear()、del
#清空陣列clear() del()
it3c = {"computer","notebook","phone"}
it3c.clear()
print(it3c) # set() 清空資料,不會清空變數

myclass = set(("java","python","rwd","ps","ai","dw"))
del myclass #變數整個被清空
print(myclass)  #找不到變數所以會引發錯誤 

看看效果

 

YiruAtStudio - 關於我