Python List – (Python教學) – 所謂List, 就是把多個同類的variable串列在一起. 加上for, foreach迴圈的使用, 令他們可以作出相同的行為.
試想想有一堆不同值的string, 如果我想把他們用同一套一個程式碼運行, 那辨法是甚麼. 一是用method, 二是用for, foreach迴圈, 當然還有其他方法. 但如果一堆string, 散落在不同的class, module裡, 那可以如何, 就是用List列表把它們放在一起, 並用method, 迴圈來實行. 這一方面增加了編程的彈性, 及減少了編程的代碼, 大大有利了對程式碼的實作及維護.
再加上Interface, Inherit Class的運用, 把各程不同的類放在一起, 可把巳有的編程碼運用得更靈活, 但對初學者來說較有難度.
本篇會介紹List的基本9種應用方法 – List初始化, List加入值, List插入值, List追加值, List更新值, List刪除值, List清除所有值, List切片,List排序, List for迴圈. Let’s Start!
方法 | 例子: |
初始化 | lst = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’] print(lst) # [‘John’, ‘Peter’, ‘Mary’, ‘Marks’] |
加入值 | lst = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’] lst.append(“Tim”); print(lst) # [‘John’, ‘Peter’, ‘Mary’, ‘Marks’, ‘Tim’] |
加入多個值1 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’]; lst2 = [‘Greenlands’, ‘Kingston’]; result = lst1 + lst2; print(result); # [‘John’, ‘Peter’, ‘Mary’, ‘Marks’, ‘Greenlands’, ‘Kingston’] |
加入多個值2 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’]; lst2 = [‘Greenlands’, ‘Kingston’] lst1.extend(lst2); print(lst1); # [‘John’, ‘Peter’, ‘Mary’, ‘Marks’, ‘Greenlands’, ‘Kingston’] |
插入值 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’]; lst2 = ‘Greenlands’ lst1.insert(1, lst2); print(lst1); # [‘John’, ‘Greenlands’, ‘Peter’, ‘Mary’, ‘Marks’] |
更新值 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’]; lst1[2] = ‘Greenlands’ print(lst1); # [‘John’, ‘Peter’, ‘Greenlands’, ‘Marks’] |
刪除值 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’]; lst2 = ‘Peter’ lst1.remove(lst2); print(lst1); # [‘John’, ‘Mary’, ‘Marks’] |
刪除值 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’]; del lst1[2] print(lst1); # [‘John’, ‘Mary’, ‘Marks’] |
清除所有值 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’]; lst1.clear() print(lst1); # [] |
切片 Slice 1 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’, ‘Kim’, ‘Groden’, ‘Guru’]; print(lst1[2:4]) # [‘Mary’, ‘Marks’] |
切片 Slice 2 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’, ‘Kim’, ‘Groden’, ‘Guru’]; print(lst1[:4]) # [‘John’, ‘Peter’, ‘Mary’, ‘Marks’] |
切片 Slice 3 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’, ‘Kim’, ‘Groden’, ‘Guru’]; print(lst1[2:]) # [‘Mary’, ‘Marks’, ‘Kim’, ‘Groden’, ‘Guru’] |
切片 Slice 4 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’, ‘Kim’, ‘Groden’, ‘Guru’]; print(lst1[-2:]) # [‘Groden’, ‘Guru’] |
切片 Slice 5 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’, ‘Kim’, ‘Groden’, ‘Guru’]; print(lst1[:-4]) # [‘John’, ‘Peter’, ‘Mary’] |
切片 Slice 6 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’, ‘Kim’, ‘Groden’, ‘Guru’]; print(lst1[:]) # [‘John’, ‘Peter’, ‘Mary’, ‘Marks’, ‘Kim’, ‘Groden’, ‘Guru’] |
排序 | lst1 = [‘John’, ‘Peter’, ‘Mary’, ‘Marks’, ‘Kim’, ‘Groden’, ‘Guru’]; lst1.sort() print(lst1) # [‘Groden’, ‘Guru’, ‘John’, ‘Kim’, ‘Marks’, ‘Mary’, ‘Peter’] |
for迴圈 | lst1 = [‘John’, ‘Peter’, ‘Mary’]; for x in lst1: print(x) # John # Peter # Mary |
(等如) | |
lst1 = [‘John’, ‘Peter’, ‘Mary’]; [print(x) for x in lst1] # John # Peter # Mary |
相關頁面
Python Set – 學會Set的6種基本應用方法 – 初始化, 加入值, 加入多個值, 刪除值, 清除所有值, for迴圈
Python Tuple – 學會Tuple的3種基本應用方法 – 初始化,切片, for迴圈
Python Dictionary – 學會Dictionary的5種基本應用方法 – 初始化, 加入值, 更新值, 刪除值, foreach迴圈
參考資料: https://docs.python.org/3/tutorial/datastructures.html