--- tags: Python學習 --- <style> .topic{ color:#fa7f20; font-weight:bold; } </style> # Python學習日記 List的增加、刪除與排序 >參考資料 - Python王者歸來 洪錦魁著 ## <span class="topic">append()</span> #### 在陣列後端加入元素 ```python= numList = [1, 2, 3] numList.append(4) numList >> [1, 2, 3, 4] ``` ## <span class="topic">insert()</span> #### 在陣列任意位置加入元素 ```python= numList = [1, 2, 3] numList.insert(1, 4) numList >> [1, 4, 2, 3] ``` ## <span class="topic">remove()</span> #### 依照value刪除陣列元素 ```python= numList = [1, 2, 3] numList.remove(1) numList >> [2, 3] ``` #### 依照index刪除陣列元素 ```python= numList = [1, 2, 3] del numList[0] numList >> [2, 3] ``` ## <span class="topic">reverse()</span> #### 將陣列反置 ```python= numList = [1, 2, 3] numList.reverse() numList >> [3, 2, 1] ``` ## <span class="topic">sort()</span> #### 將陣列由小到大排列 ```python= numList = [3, 2, 1] numList.sort() numList >> [1, 2, 3] ``` #### 將陣列由大到小排列 ```python= numList = [1, 2, 3] numList.sort(reverse = True) numList >> [3, 2, 1] ``` ## <span class="topic">sorted()</span> #### 將陣列由小到大排列,不寫入原先陣列的數值 ```python= numList = [3, 2, 1] numList2 = numList.sorted() numList nunList2 >> [3, 2, 1] >> [1, 2, 3] ```