Python 資料結構
1.Tuple 內容不可變、功能少、占空間小(若不希望內容被更動到時使用)

x = (10,12)
x
x[0] = 12 #無法更改內容
x = (10,11)
y = list(x)
y[0] = 20
y

2. 串列(List)內容可變、功能多(常用)

slist = []
slist = ['A','B','C','D','E']
slist

slist[0] = 'B'
slist

#雙重List
dlist = [['A','B','C','D','E'],[1,2,3,4,5]]
dlist[1]
dlist[1][0]
#在最後增加單一個項目 => append
slist = ['A','B','C','D','E']
slist.append('F')
slist
#在最後增加一個串列 => extend
slist.extend(['G','H','I'])
slist
#在指定位置插入一個項目 => insert
slist.insert(1,'0') #insert(位置,item)
slist
#刪除指定位置項目 => del
del slist[1]
slist
#移出(同時刪除)串列項目=> pop
plist = ['A','B','C','D','E']
plist.pop() #預設移出最後一項 = pop(-1) , 若pop(0)則是移出開頭項
plist
#找出項目值的位置 => index
plist = ['A','B','B','C','D','E']
plist.index('B') #只會找出第一個出現的位置
#判斷項目是否存在於list中 => in
clist = ['A','B','C','D','E']
'B' in clist
'F' in clist
#排序(Sorting)
wlist = ['D','A','F','C','E','D']
sorted(wlist)
nlist = [4,23,1,3,2,98,3]
sorted(nlist)

3. 字典(Dictionary)

dic = {'Jack':84,'Ben':63,'Cathy':76, 'Bob': 83}
dic
dic['Jack']
dic = {'Jack':[84,23,34],'Ben':[63,12,74],'Cathy':[12,43,76],'Bob': [83,81,90]}
dic
dic['Jack']
dic['Jack'] = [1,2,3]
dic
#新增 /合併字典 => update
dic.update({'William':[23,43,84],'Eric':[93,31,32]})
dic
#刪除項目 => del
del dic['William']
dic
#判斷 key是否存在
'Ben' in dic
'William' in dic
#取得所有的 key
dic.keys() #not list
list(dic.keys())
#取得所有的 value
dic.values() #not list
list(dic.values())
#取得所有的key value pair
dic.items() #not list
list(dic.items())

4. 集合(Set)

set1 = set()
set1 = {1,2,3,4,4,4,5}
set1
#判斷是否在集合中
2 in set1
set2 = {3,4,5,6,6,7}
set2
#set項目數
len(set1)
# 最大值
max(set1)
# 最小值
min(set1)
#增減項目
#增加
set1.add(6)
set1
#減少
set1.remove(6)
set1
#集合運算
#交集
set1&set2 #相等於set1.intersection(set2)
#聯集
set1|set2 #相等於set1.union(set2)
#差集
set1-set2 #相等於set1.difference(set2)
# 真子集
set1 = {3,4,5}
set2 = {1,2,3,4,5}
set1 set1<=set2 #相等於set1.issubset(set2)
# 母集/超集合
set1>=set2
# 判斷是否"無"交集
set1.isdisjoint(set2)