# List 2022 資訊之芽北區py班 --- ## 來記一些資訊好了 ---- ```python= student1 = 100 ``` <span>沒什麼問題,那就加一個<!-- .element: class="fragment" data-fragment-index="1" --></span> ---- ```python= student1 = 100 student2 = 87 ``` <span>輕鬆簡單,那就再加幾個<!-- .element: class="fragment" data-fragment-index="1" --></span> ---- ```python= student1 = 100 student2 = 87 student3 = 77 student4 = 69 ``` <span>如果有10個? 20個? 100個?<!-- .element: class="fragment" data-fragment-index="1" --></span> --- ## List ---- - iterable - 可以重複 - 有順序 ---- 用法 ```python= l = [] #一個空的list ``` ---- ![](https://i.imgur.com/OKz37A7.jpg) 基本上要塞甚麼都可以 ```python= list1 = [905, "giveYouUp", False, [1987, 7, 27], {3.14}, 905] list2 = [list1] ``` ---- ![](https://i.imgur.com/eIztFn0.jpg) 有多大? ```python= list1 = [] print(len(list1)) #0 list1 = [1, 2, 3] print(len(list1)) #3 ``` ---- 猜猜看這會印出什麼 ```python= l = [1, 2, 3, [1, 2, 3], []] print(len(l)) ``` ---- list裡面的東西要怎麼取? ```python= l = ["a", "b", "c", "d"] print(l[0]) #a print(l[3]) #d print(l[-1]) #d ``` ---- 也可以一次取好幾個 `list[start:end:step]` 包含`start`不包含`end` 首項,結尾,公差 ![](https://i.imgur.com/OSRy3ht.jpg) ```python= l = ["a", "b", "c", "d", "e", "f", "g"] print(l[1:3]) #["b", "c"] print(l[0:-1]) #["a", "b", "c", "d", "e", "f"] print(l[0:5:2]) #["a", "c", "e"] ``` ---- 要怎麼修改 ```python= l = ["a", "b", "c", "d"] l[2] = "z" # ["a", "b", "z", "d"] ``` ---- 這樣會發生什麼事 ```python= l = [1, 2, 3, 4, 5, 6, 7] l[1:4] = [10, 11, 12, 13] ``` --- ## list的method ---- append 加一項在最後面 ```python= l = [1, 2, 3] l.append(10) # [1, 2, 3, 10] ``` ---- extend 加一群在最後面 ```python= l = [1, 2, 3] l.extend([4, 5, 6]) # [1, 2, 3, 4, 5, 6] ``` ---- 分別會變成怎樣 ```python= l = [1, 2, 3] l.append([4, 5, 6]) ``` ```python= l = [1, 2, 3] l.extend([4, 5, 6]) ``` ---- append v.s. extend ```python= l = [1, 2, 3] l.append([4, 5, 6]) # [1, 2, 3, [4, 5, 6]] ``` ```python= l = [1, 2, 3] l.extend([4, 5, 6]) # [1, 2, 3, 4, 5, 6] ``` ---- insert(index, object) 加一個在你想要的位置 ```python= l = [1, 2, 3, 4] l.insert(3, 7) # [1, 2, 3, 7, 4] l.insert(100, 9) # [1, 2, 3, 7, 4, 9] ``` ---- pop() 回傳最後一項並刪掉 ```python= l = [1, 2, 3, 4] print(l.pop()) # 4 l = [] print(l.pop()) # IndexError ``` ---- pop(index) 回傳第index個並刪掉 ```python= l = ['a', 'b', 'c', 'd'] print(l.pop(2)) # 'c' print(l.pop(10)) # IndexError ``` ---- remove(obj) 刪掉第一個obj ```python= l = ['a', 'b', 'c', 'd', 'a', 'a'] l.remove('a') # ['b', 'c', 'd', 'a', 'a'] l.remove('z') # ValueError ``` ---- clear() 全部清空 ![](https://i.imgur.com/I6XOyP6.jpg) ```python= l = [1, 2, 3] l.clear() # [] ``` ---- count(obj) 數obj出現幾次 ```python= l = [1, 1, 2, 3, 5, 8] print(l.count(1)) # 2 print(l.count(47)) # 0 ``` ---- index(obj) index(obj, start) index(obj, start, end) 回傳從start到end出現的第一個obj的index ```python= l = [1, 2, 3, 1, 1, 2, 'no'] print(l.index(1)) # 0 print(l.index(1, 2)) # 3 print(l.index(1, 4, 6)) # 4 print(l.index("yes")) # ValueError ``` ---- sort(), reverse() 顧名思義就是排序跟反轉 ```python= l = [1, 9, 2, 8, 3, 7, 4, 6, 5] l.sort() # [1, 2, 3, 4, 5, 6, 7, 8, 9] l.reverse() # [9, 8, 7, 6, 5, 4, 3, 2, 1] ``` ---- 會輸出甚麼 ```python= a = [1, 2, 3, 4] b = a a[0] = 5 print(a) print(b) ``` ---- copy() ![](https://i.imgur.com/us9bXOb.jpg) ```python= a = [1, 2, 3, 4] b = a # [1, 2, 3, 4] c = a.copy() # [1, 2, 3, 4] d = a[:] # [1, 2, 3, 4] a[0] = 5 # [5, 2, 3, 4] print(b) # [5, 2, 3, 4] print(c) # [1, 2, 3, 4] print(d) # [1, 2, 3, 4] ``` --- ## 所以現在是練習時間 [自動](https://neoj.sprout.tw/problem/2009/)[點](https://www.youtube.com/watch?v=dQw4w9WgXcQ)[名機](https://neoj.sprout.tw/problem/2009/) --- ## list有關的function ---- list(iterable) ```python= print(list(range(5))) # [0, 1, 2, 3, 4] ``` ---- max(l), min(l), sum(l) ```python= l = [1, 2, 3] print(max(l)) # 3 print(min(l)) # 1 print(sum(l)) # 6 ``` ---- sorted(l) 回傳一個排好的list ```python= l = [1, 9, 2, 8, 3, 7, 4, 6, 5] a = sorted(l) print(l) # [1, 9, 2, 8, 3, 7, 4, 6, 5] print(a) # [1, 2, 3, 4, 5, 6, 7, 8, 9] b = l.sort() print(l) # [1, 2, 3, 4, 5, 6, 7, 8, 9] print(b) # None ``` ---- map(func, iterable) ![](https://i.imgur.com/Jjqboaj.png) 他回傳的是一個map object! 不是list! ```python= char_list = ['1', '2', '3', '4'] print(map(int, char_list)) # <map object at ......> print(list(map(int, char_list))) # [1, 2, 3, 4] for i in map(int, char_list): print(i) ``` --- ## 跟for搭配 ---- ```python= ls = [1, 2, 3, 4, 6, 7, 8, 9] for i in ls: print(i) ``` ---- ```python= ls = [1, 2, 3, 4, 6, 7, 8, 9] for i in range(len(ls)): print(i, ls[i]) ``` ---- ```python= ls = [1, 2, 3, 4, 6, 7, 8, 9] for i, l in enumerate(ls): print(i, l) ``` ---- :chestnut: :`grade`這個list裡面有全班的成績,請把`grade`裡面所有人的成績改成平方後除以100 ```python= grade = [100, 87, 78, 99, 47, 59, 39.49] ``` ---- :rice: 解 ```python= for i, j in enumerate(grade): grade[i] = j**2/100 ``` ```python= for i in range(len(grade)): grade[i] = grade[i]**2/100 ``` ---- 常見錯誤 ```python= for i in grade: i = i**2/100 ``` --- ## 練習題 [陳刀與](https://neoj.sprout.tw/problem/327/)[西](https://www.youtube.com/watch?v=dQw4w9WgXcQ)[瓜田 III](https://neoj.sprout.tw/problem/327/) --- ## 作業 [修羅道](https://neoj.sprout.tw/problem/2005/)[之](https://www.youtube.com/watch?v=dQw4w9WgXcQ)[佛](https://neoj.sprout.tw/problem/2005/) --- ## 延伸閱讀 `help(list)` [copy](https://blog.csdn.net/u011995719/article/details/82911392) [有趣的東西](https://www.youtube.com/watch?v=dQw4w9WgXcQ&t) [好康的](https://youtu.be/ew_eAR4igNs?t=615)
{"metaMigratedAt":"2023-06-16T21:01:04.626Z","metaMigratedFrom":"YAML","title":"List","breaks":true,"contributors":"[{\"id\":\"54bbdba3-ffbf-423a-b026-751cb8a77149\",\"add\":6691,\"del\":1189}]"}
    908 views
   owned this note