Container
容器
By Rex Wu && 小草
容器?
-
陣列
-
關鍵字對應(key-value)
-
queue
-
stack
Container Datatypes
-
list
-
tuple
-
dict (dictionary)
-
set
list
create a list
list1 = []
list2 = list()
nameList = ['Tom', 'John', 'Rex']
intList = [1, 2, 3, 4, 5]
floatList = [1.1, 2.2, 3.3, 4.4, 5.5]
mixList = ['Tom', 2, 3.3]
other way create list
list('cat')
tuple1 = ('dog', 'cat', 'fish')
list(tuple1)
date = '2019/11/13'
date.split('/')
輸出串列中的項目
list = ['dog', 'cat', 'fish', 'bird']
list[0] #dog
list[-1] #bird
以slice輸出
list = ['dog', 'cat', 'fish', 'bird']
list[0:2] #['dog','cat']
list[::2] #['dog','fish']
list[::-1] #['bird','fish','cat','dog']
增加串列中的項目
list = ['dog', 'cat', 'fish', 'bird']
list[2] = 'pig'
#'fish' => 'pig'
list.append('cow')
# ['dog','cat','pig','bird','cow']
list.insert(3, 'sheep')
# ['dog','cat','pig','sheep','bird','cow']
減少串列中的項目
list.remove('bird')
# ['dog','cat','pig','sheep','cow']
list.pop()
# ['dog','cat','pig','sheep'] & return cow
del list[0]
# ['cat','pig','sheep']
list.clear()
other function
# ['cat','pig','sheep']
list.index('sheep') # 2
'pig' in list # True
'fish' in list # False
list.count('pig') # 1
len(list) # 3
other function
numList = [3,1,10,5,2]
numList.sort()
numList.sort(reverse=True)
Try it
list = [1,[2,3],4]
# 如何輸出 2 or 3
串列中的串列
list[1][0] # 2
list[1][1] # 3
挑戰
輸入一個陣列:
取最大值
取最小值
取中位數
(提示:for)
tuple
create tuple
tuples = 'dog', 'cat', 'fish'
a, b, c = tuples
list = ['dog', 'cat', 'fish']
tuple(list)
tuple vs list
- tuple 中的項目是不可變更
- list 想怎麼改就怎麼改
- list 比 tuple 更常使用到
簡單來說 tuple 就是不可變更的 list
dict
(dictionary)
dict - 字典
-
key - value
-
value 可以有多種型態
-
key 只能為 string
dict - 字典 可以做什麼
-
個人檔案
-
摩斯密碼對照(密碼: 明碼)
-
菜單(餐點: 價錢)
-
Others...?
dict looks like this
{"name": "小草", "height": 180, "weight": None}
"name": "小草"
key
value
create a dict
iterator keys
for key in dict:
print(key)
iterator keys
and store values in a list
list = []
for key in dict:
list.append(dict[key])
adding items
dict.update({"nickname": "falcon",
"comment": "I like python"})
dict = {"name": "小草", "height": 180, "weight": 76}
dict.update(dict2)
editing a value
dict["weight"] = 76
dict = {"name": "小草", "height": 180, "weight": None,
"other": None}
dict[key] = value
deleting a value
del dict["other"]
dict = {"name": "小草", "height": 180, "weight": 76,
"other": None}
del dict[key]
get all keys / values
dict.keys()
dict.values()
get value by key
value = dict.get(key)
value = dict[key]
Other functions
dict.pop(key) #Pop an item by key
dict.popitem() # FILO
dict.setdefault(key, value)
dict.clear()
建立一個 dictionary 並包含
挑戰
-
名字
-
身高
-
年齡
-
一句你想說的話
並輸出所有的Value
set
-
值不重複
-
set 內可以有多種型態
-
交集、連集、差集運算
set
set looks like this
{1, 2, 3, 4}
s = set()
create a set
a = {1, 2, 3}
b = {2, 4, 6}
s = set()
add value
s.add(value)
remove value
s.discard(value)
pop value (randomly)
s.pop()
boolean algebra
a.union(b) # = a | b
a.intersection(b) # = a & b
a.difference(b) # = a - b
a.symmetric_difference(b) # = a ^ b
a.issubset(b)
other
sum(s)
len(s)
min(s)
max(s)
demo
建立 set A 與 set B
且 A 是 B 的子集合
Combine Them All !
目標:建立一個朋友名單
Hint: 使用 list,裡面包含有朋友資訊的 dict
Combine Them All !
[ {"key": "value"}, (0, 1), {1, 2, 3} ]
題目1
稿費問題
你是海大新聞的總編輯。有人投稿了一篇文章,你決定要使用在下週的報紙上,請你使用Python寫出個計算稿費的程式算出要給投稿者的稿費~
計費方式如下,大寫不計費
小寫部分只計算母音,價目表如下
a: 2元 e: 2元 i: 3元 o: 3元 u: 8元
deck
By Rex Wu
deck
- 259