# 容器 ###### tags: `python` ## 串列 List 今天媽咪請你去全聯買東西,你可以用程式把它記錄下來: ```python= shoppingList = "雞蛋\n米酒\n" shoppingList += "鮮奶\n" + "麵條\n" print(shoppingList) # output: # 雞蛋 # 米酒 # 鮮奶 # 麵條 ``` 這樣看來似乎可行,但試想: - 如果你要移除某一項? - 如果這個清單增加到數百個,第24個東西是什麼? - 總共有幾項商品需要買? 這時候我們可以使用「串列」: ```python= shoppingList = ["雞蛋", "米酒", "鮮奶"] # 增加項目 shoppingList.append("麵條") print(shoppingList) # output: ["雞蛋", "米酒", "鮮奶", "麵條"] # 合併另一個串列 anotherList = ["洋芋片", "醬油"] shoppingList += anotherList print(shoppingList) # output: ["雞蛋", "米酒", "鮮奶", "麵條", "洋芋片", "醬油"] ``` - 在第一行,我們用中括號來代表串列,並且每個項目用逗號隔開。 - 在第四行,如果要新增項目,我們可以用`append()`函數。 這個函數是串列的函數,我們在這個串列後面加一個句號然後再接著函數。 - 在第九行我們新增另一個串列。並且在第十行把兩個串列加起來合併。 接著,如果我們想知道特定一個值,可以使用他的索引來找他。 在python中,索引從0開始: ```python= shoppingList = ["雞蛋", "米酒", "鮮奶", "麵條", "洋芋片", "醬油"] print(shoppingList[0]) # output: "雞蛋" print(shoppingList[5]) # output: "醬油" ``` 一些常用的方法: ```python= shoppingList = ["雞蛋", "米酒", "雞蛋", "鮮奶", "麵條", "洋芋片", "醬油"] # 更改特定位置的值 shoppingList[1] = "小米酒" print(shoppingList) # output: ["雞蛋", "小米酒", "雞蛋", "鮮奶", "麵條", "洋芋片", "醬油"] # 計算特定值出現幾次 shoppingList.count("雞蛋") # 2 # 回傳該值第一次出現的索引(沒有該值會出現錯誤) shoppingList.index("雞蛋") # 0 # 在特定索引插入值 shoppingList.insert(2,"奶油") print(shoppingList) # output: ["雞蛋", "米酒", "奶油", "雞蛋", "鮮奶", "麵條", "洋芋片", "醬油"] # 刪除第一個出現的特定值 shoppingList.remove("雞蛋") print(shoppingList) # output: ["米酒", "奶油", "雞蛋", "鮮奶", "麵條", "洋芋片", "醬油"] # 把整個串列反序 shoppingList.reverse() print(shoppingList) # output: ['醬油', '洋芋片', '麵條', '鮮奶', '雞蛋', '奶油', '米酒'] # 排序(字串的話是字典排序;數字的話是小到大) shoppingList.sort() # 查看某個值是否在該串列中 "醬油" in shoppingList # True "油" in shoppingList # False # 清除所有值 shoppingList.clear() print(shoppingList) # output: [] ``` ## Tuple 除了串列之外,也有其他的容器。初學這邊可以先跳過。 我們會用小括號來代表一個tuple,tuple的值是不可以更改的。 ```python= someTuple = (0,1,2,3,4,5) someTuple[0] = 99 # TypeError: 'tuple' object does not support item assignment ``` ### Unpack tuple We can assgin some varibles by unpacking tuple: ```python= myName, MyAge = ("Kyle", 25) print(f'My name is {myName}. I\'m {MyAge} years old.') # My name is Kyle. I'm 25 years old. ``` Now let's think a situaion that we need to swap two number: ```python= # Ordinary way: x, y = 1, 2 # define a temperary varible to store one of them. temp = x x = y y = temp print(f'x = {x}, y = {y}') # x = 2, y = 1 ``` With tech of unpacking tuple, we can easily do like this: ```python= x, y = 1, 2 x, y = y, x print(f'x = {x}, y = {y}') # x = 2, y = 1 ``` So simple right? Try it yourself! ## Dictionary 字典用大括號來表示,它是一個「`key`跟`value`」的對應: ```python= introduction = {"外號": "高雄金城武", "身高": 183} print(introduction["外號"]) # output: "高雄金城武" ``` `key` 在整個字典裡要是唯一的,不過`value`可以重複。就好像菜單每個餐的名字不會重複(key),但金額可以重複(value)。 ```python= menu = {'1號餐':89, '2號餐':89} ``` 除了直接用中括號來當作索引,我們也可以用`get`來取得內容。 用`get`的話,可以設定在找不到東西時的`default`預設值。 ```python= menu = {'1號餐':89, '2號餐':89} print(menu.get('2號餐', '查詢不到餐點')) # 89 print(menu.get('3號餐', '查詢不到餐點')) # '查詢不到餐點' ``` 查詢更多`dictionary`的用法 ```python= help(dict) ``` 字典迭代(iteration) ```python= menu = {'1號餐':89, '2號餐':89} for meal, price in menu.items(): print(f'{meal}:{price}元。') # 1號餐:89元。 # 2號餐:89元。 ``` [用 Dictionary 做文字RPG](/H4F7mOEjThCkmIUh4ANMvQ) ## Set Todo...