# 串列(list)與字典(dict) 黃安聖 https://enn.design/python/3 ###### tags: `Python程式設計` ---- ## 取得今日練習檔案 https://colab.research.google.com/drive/1MvhuLTZnZlnMOpzAwcc7GSCR3LYc-46A?usp=sharing ---- ## 資料型別 - 串列(list) - 一群資料的群組 - 具有順序性 ---- ```python= # 建立一個串列 numbers = [2,3,20,15,20,30] print(numbers) # 2,3,20,15,20,30 type(numbers) # list ``` ---- ## 用索引取得串列內的資料 (由0開始數) ```python lottery_numbers = [3,6,12,18,22,25,30] # 串列索引順序: 0,1,2 ,3 ,4 ,5 ,6 # 印出串列第1筆資料 print(lottery_numbers[0]) # 印出串列第2筆資料 print(lottery_numbers[1]) # 印出串列第3筆資料 print(lottery_numbers[2]) # 依此類推..... ``` ---- ## 用.sort()整理串列內的數字 串列內必須為數字 ```python= numbers = [80,52,73,2,15,7.5,-5] # 用.sort()將由小至大排列 numbers.sort() print(numbers) ``` ---- ## 用len()取得串列資料長度 ```python= brand_list = ['Nissan','Lexus','Audi','BMW','Honda','Volkswagen'] # 用len() 取得串列內有多少筆資料 print(len(brand_list)) # 將會得到 6 print(type(len(brand_list))) # 透過len取得的串列長度型別為整數,方便之後做計算 ``` ---- ## 用append()加入新資料 ```python= my_list = [1,2,3,4] print(my_list) # 從串列最後面加入資料 5 my_list.append(5) print(my_list) ``` ---- ## 用insert(index,data)在指定索引插入資料 ```python= my_list = [1,2,3,4] # 在索引3的位置插入資料2.5 my_list.insert(3,3.5) print(my_list) # [1,2,3,3.5,4] ``` ---- ## 刪除串列資料(ㄧ):pop() ```python= list1 = [1,2,3,4,5] # 如果pop()沒有傳入值將預設刪除最後一筆資料 list1.pop() print(list1) # [1,2,3,4] ``` ---- ## 刪除串列資料(ㄧ):pop() ```python= list1 = [1,2,3,4,5] # 如果pop()有傳入值將刪除指定索引資料 list1.pop(0) print(list1) # [2,3,4,5] ``` ---- ## 刪除串列資料(二):remove() ```python= name_list = ['Andy','Jack','Tony','Jack'] name_list.remove('Jack') print(name_list) # ['Andy','Tony','Jack'] ``` ---- ## 用clear清空串列 ```python= list1 = [1,2,3,4,5] list1.clear() print(list1) # [] ``` ---- ## 串列小技巧:用.index()取得指定資料的索引 ```python= # 建立name_list name_list = ['Tom', 'Andy', 'Jeffery', 'Abby'] # 取得資料'abby'的索引位置 abby_index = name_list.index('Abby') # 印出abby_index print(abby_index) # 3 ``` ---- ## 串列小技巧:用.count()取得資料出現的次數 ```python= number_list = [2,3,3,3,4,3,1,5,2] # 印出number_list裡面有幾個1 print(number_list.count(1)) # 1 # 印出number_list裡面有幾個2 print(number_list.count(2)) # 2 # 印出number_list裡面有幾個3 print(number_list.count(3)) # 4 # 印出number_list裡面有幾個6 print(number_list.count(6)) # 0 ``` ---- ## 資料型別 - 字典(dict) - 適合形容一組複雜資料 - 鍵(Key):值(Value)配對組合 ---- ```python= # 定義一個user1的字典 user1 = { 'name': 'Andy', 'age': 27, 'city': 'Taipei' } # 印出user1的資料 print(user1) # 印出user1的型別 print(type(user1)) ``` ---- ## 透過鍵(key)取得值(value) ```python= # 定義一個user1的字典 user1 = { 'name': 'Andy', 'age': 27, 'city': 'Taipei' } # 印出user1的資料 print(user1['name']) # Andy print(user1['age']) # 27 ``` ---- ## 新增資料至字典(dict)中 ```python= # 在user1裡,新增key為'email', value為'tony@gmail.com'的資料 user1['email'] = 'andy@gmail.com' # 在user1裡,新增key為'login', value為True的資料 user1['login'] = True print(user1) ``` ---- ## 可以新增,就可以透過Key修改 ```python= # 更改使用者狀態為登出 user1['login'] = False print(user1) ``` ---- ## ㄧ樣可用.pop(key)刪除資料 ```python= user1 = { 'name': 'Andy', 'age': 27, 'city': 'Taipei' } # 刪除'age':20這筆資料 user1.pop('age') print(user1) # 將沒有age的資料 ``` ---- ### 隨堂練習 試著取得以下使用者資料的**好友人數** ```python= user1 = { "name": "John", "age": 30, "friends": [ "Ben", "Eric", "Abby", "Joanne", "Philips" ] } ``` ---- ### 透過證交所基本市況API取得資料 https://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=tse_2330.tw&json=1&delay=0 ---- Python的各種括號 https://gist.github.com/andy19910102/da7934a385417f952b90987d39954a1d#file-python-py ---- ```python= # 小括號 () # 呼叫一個函式。例如:print() input() abs(-55)... # 表達一個元組。例如:student = ("Eric", 22) # 中括號 [] # 表達一個串列。例如:nums = [10, 20, 30] # 透過索引取得串列中的值。例如:nums[0] # 透過key取得字典中的值。例如:product["name"] # 大括號 {} # 表達一個字典。例如: product = {"name": "iPhone 15", "price": 30000} # 表達一個集合。例如: fruits = {"apple", "banana", "cherry"} # f-string中插入變數。例如 f"Hi 我的名字是{name}" ```
{"metaMigratedAt":"2023-06-14T16:21:47.420Z","metaMigratedFrom":"YAML","breaks":true,"slideOptions":"{\"mouseWheel\":true,\"width\":\"100%\",\"height\":\"90%\",\"margin\":0.1,\"minScale\":1,\"maxScale\":2,\"loop\":true}","description":"黃安聖","title":"串列(list)與字典(dict)","contributors":"[{\"id\":\"29b6dbac-bda4-4060-80ed-e1f3b73fafeb\",\"add\":2257,\"del\":3394,\"latestUpdatedAt\":1752987051686}]"}
    10648 views