【Python】Python - 存取Set items。Python Access Set Items.
【Python】Python - 存取Set items。Python Access Set Items.
存取 Items
你無法使用index或key去存取set裡面的items。
但可以使用for 迴圈整個讀取,或使用特定的值去用in keyword。
範例
使用迴圈透過set 並列值:
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)上面輸出,順序不一定: applecherrybanana範例
判斷是否 "banana" 有存在set裡面:
thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)上面輸出: True修改 Items
一旦set被建立後,你無法改變裡面的items,但你可以使用新增add(),來增加新的items。

留言
張貼留言