set() 使用說明
set() 三大特點
- 無序號
- 無索引(隨機出現)
- 無重覆(一樣的只會出現一次)
一筆筆的讀資料 : for 迴圈
抓出set()總共有幾筆資料:len()
現有陣列上添加一筆資料:add()
現有的陣列上添加多筆資料:update()
刪除資料元素 discard() or remove()
清空陣列 clear()、del
查詢陣列中有沒有我要的資料: in (只回傳true 或 false)
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'}
刪除資料元素 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) #找不到變數所以會引發錯誤
刪除多筆資料
a={"a","b","c","d","e"}
b={"b","c"}
print(a) #{"a","b","c","d","e"}
for x in b:
a.remove(x)
print(a)#{"a","d","e"}
讓使用者輸入要刪除的元素
a = {"a", "b", "c", "d", "e"}
print("a=",a)
# 讓使用者輸入要刪除的元素
b = set()
while True:
x = input("請輸入要刪除的元素 (-9999 結束輸入): ")
if x == "-9999":
break
b.add(x)
# 將 b set 中的元素從 a set 中刪除
for x in b:
a.discard(x)
print("a=",a)
另外一種方式
a = {"a", "b", "c", "d", "e"}
print("a=",a)
# 讓使用者輸入要刪除的元素
b = set()
b = input("請輸入要刪除的元素") #abc
print("b",b) #["a","b","c"]
# 將 b set 中的元素從 a set 中刪除
for x in b:
print(x)#a,c,b
a.discard(x)#a.discard(b)
print("刪除後",a)
print("a=",a)
查詢陣列中有沒有我要的資料
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
Yiru@Studio - 關於我 - 意如