# List
2024 資訊之芽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=
list1 = [] # 一個空的list
list2 = list() # 另一個空的list
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
```
----
為什麼是從0? 🌳🐒
數字代表「位移量」,不是「第幾個」
`l[n]`:從開頭位移n個位置
----
也可以一次取好幾個
`list[start:]`
`list[:end]`
`list[start:end:step]`
首項(含),尾項(不含),公差(預設1)
<!-- ![](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"]
```
[有關每項預設值(補充)](https://docs.python.org/dev/library/stdtypes.html#sequence-types-list-tuple-range)
----
grades 裡面有全班的成績,請把前三個人的成績印出來,也把第偶數個人的成績印出來
```python=
grades = [100, 87, 78, 99, 47, 59, 39.49]
```
----
:rice:解
```python=
print(grades[:3])
print(grades[1::2])
```
----
要怎麼修改
```python=
l = ["a", "b", "c", "d"]
l[2] = "z" # ["a", "b", "z", "d"]
l[1:3] = ["x", "y"] # ["a", "x", "y", "d"]
```
----
這樣會發生什麼事
```python=
l = [1, 2, 3, 4, 5, 6, 7]
l[1:3] = [10, 11, 12, 13]
```
----
加法乘法
分別會輸出什麼
```python=
l = [1, 2, 3]
print(l + [4])
print(l + 4)
print(l * [2])
print(l * 2)
print(l)
```
----
如果真的要改的話
```python=
l = l + [4]
l += [5]
l *= 2
```
----
要怎麼知道值有沒有在list裡面
```python=
l = [1, 2, 3, 4, 5]
for i in l:
if i == 3:
print(True)
break
else:
print(False)
```
----
```python=
l = [1, 2, 3, 4, 5]
print(3 in l) # True
print(6 in l) # False
```
---
## 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
print(l) # [1, 2, 3]
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]
l2 = l[::-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
c = a.copy()
d = a[:]
a[0] = 5
print(b) # [5, 2, 3, 4]
print(c) # [1, 2, 3, 4]
print(d) # [1, 2, 3, 4]
```
---
## 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
```
如果不用內建的function要怎麼做
----
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
```
----
reversed(l)
回傳一個反過來的list
```python=
l = [1, 8, 2, 8, 3, 7, 4, 6, 5]
a = reversed(l)
print(l) # [1, 9, 2, 8, 3, 7, 4, 6, 5]
print(a) # [5, 6, 4, 7, 3, 8, 2, 9, 1]
b = l.reverse()
print(l) # [5, 6, 4, 7, 3, 8, 2, 9, 1]
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, v in enumerate(ls):
print(i, v)
```
----
: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/2005/)[之](https://www.youtube.com/watch?v=dQw4w9WgXcQ)[佛](https://neoj.sprout.tw/problem/2005/)
[自動](https://neoj.sprout.tw/problem/2009/)[點](https://www.youtube.com/watch?v=dQw4w9WgXcQ)[名機](https://neoj.sprout.tw/problem/2009/)
---
## 延伸閱讀
`help(list)`
[copy](https://blog.csdn.net/u011995719/article/details/82911392)
[有趣的東西](https://www.youtube.com/watch?v=dQw4w9WgXcQ&t)
[deepcopy](https://docs.python.org/3/library/copy.html)
{"title":"List 2024","contributors":"[{\"id\":\"54bbdba3-ffbf-423a-b026-751cb8a77149\",\"add\":6850,\"del\":418}]"}