[PYTHON] List 篇
< Assign >
# 指定空 list
lst = []
lst = list()
# 指定有元素的 list
# 型態可不一樣
lst = [1, '1', 1.0]
lst = [i for i in range(5)] # [0, 1, 2, 3, 4]
lst = [0] * 5 # [0, 0, 0, 0, 0] # 不可這樣:lst = [[]] * 5, 址會一樣
lst = [int(x) for x in input().split()]
< Index and Value >
index 索引值:
- 主要作用為取值和修改。
- 範圍: 1 ~ len(list) − 1,大於 len(list) − 1 會發生錯誤。
- 範圍:−1 ~ −len(list),小於 −len(list) 會發生錯誤。
- 為 index 所對應到的值。
- 可為任意型態。
用法:
lst = [2, '4', 6.0]
print(lst[0]) # index: 0, value: 2
print(lst[1]) # index: 1, value: '4'
print(lst[2]) # index: 2, value: 6.0
< Slice >
- 取片段 list。
- lst[ start: stop: step ],start 預設是 0,stop 預設是 len(list),step 預設是 1。
lst = [1, 2, 3, 4, 5]
print(lst[3:]) # [4, 5] # 從 index: 3 開始到結束
print(lst[: 3]) # [1, 2, 3] # 從頭開始到 index: 3 結束
print(lst[:: 2]) # [1, 3, 5] # 從頭到尾, 間隔為 2
- append(object):將 object 加到 list 的末尾。
lst = []
print(lst) # []
lst.append(1)
print(lst) # [1]
lst.append(2)
print(lst) # [1, 2]
lst.append(3)
print(lst) # [1, 2, 3]
- clear():從 list 刪除所有項目。
lst = [1, 2, 3]
lst.clear()
print(lst) # []
- copy():回傳一個淺複製的 list。
# 沒有使用 copy()
lst_1 = [1, 2, 3]
lst_2 = lst_1
lst_2[0] = 100 # 更改元素
print(lst_1) # [100, 2, 3]
print(lst_2) # [100, 2, 3]
# 使用 copy()
lst_1 = [1, 2, 3]
lst_2 = lst_1.copy()
lst_2[0] = 100 # 更改元素
print(lst_1) # [1, 2, 3]
print(lst_2) # [100, 2, 3]
- count(value):回傳 value 出現的次數。
lst = [1, 2, 2, 3, 3, 3]
print(lst.count(1)) # 1
print(lst.count(2)) # 2
print(lst.count(3)) # 3
print(lst.count(4)) # 0
- extend(iterable):透過附加 iterable 的元素來擴展 list。
lst = [1, 2]
lst.extend([3, 4])
print(lst) # [1, 2, 3, 4]
- index(value):回傳 value 在 list 的第一個 index,若 value 不在 list,則會發生錯誤。
lst = [1, 2, 4]
print(lst.index(1)) # 0
print(lst.index(2)) # 1
print(lst.index(3)) # ValueError: 3 is not in list
- insert(index, object):插入 object 在 index 之前。
lst = [0, 2, 8]
lst.insert(2, 4)
print(lst) # [0, 2, 4, 8]
lst.insert(3, 6)
print(lst) # [0, 2, 4, 6, 8]
- pop():若無參數,則刪除尾端元素;反之,刪除指定 index 的元素。若指定 index 超過 list 的長度則會發生錯誤。對於空的 list 使用 pop() 亦會發生錯誤。若成功執行,會回傳刪除的元素。
lst = [1, 2, 3, 4, 5]
print(lst.pop()) # 5
print(lst) # [1, 2, 3, 4]
lst = [1, 2, 3, 4, 5]
print(lst.pop(0)) # 1
print(lst) # [2, 3, 4, 5]
- remove(value):刪除 value 在 list 第一次出現的值。如果 value 不在 list 則會發生錯誤
lst = [1, 2, 2, 3]
lst.remove(2)
print(lst) # [1, 2, 3]
lst = [1, 2, 3]
lst.remove(4) # ValueError: list.remove(x): x not in list
- reverse():將 list 反轉。
lst = [1, 2, 3]
lst.reverse()
print(lst) # [3, 2, 1]
- sort():數字型態不可和字串形態排序。若無參數,則將 list 由小到大排序。參數可有 key、reverse,key可以依照喜好方法排序,reverse 預設是 False,可以改成 True,設為由大到小排序。
lst = [5, 1, 3, 2, 4]
lst.sort()
print(lst) # [1, 2, 3, 4, 5]
lst = [5, 1, 3, 2, 4]
lst.sort(reverse = True) # 由大到小排序
print(lst) # [5, 4, 3, 2, 1]
lst = [5, 1, 3, 2, 4]
lst.sort(key = lambda z: (z % 3, z)) # 根據 (z % 3, z) 排序
print(lst) # [3, 1, 4, 2, 5]
< Function >
- len(list):回傳 list 的長度。
lst = [1, 2, 3, 4, 5]
print(len(lst)) # 5
- max(list):回傳 list 的最大值。
lst = [1, 2, 3, 4, 5]
print(max(lst)) # 5
- min(list):回傳 list 的最小值。
lst = [1, 2, 3, 4, 5]
print(min(lst)) # 1
- sum(list):回傳 list 的總和。
lst = [1, 2, 3, 4, 5]
print(sum(lst)) # 15
< Traversal >
- 使用 range()、len()、index。
lst = [1, 2, 3, 4, 5]
for i in range(len(lst)):
print('index:', i, 'value:', lst[i])
'''
index: 0 value: 1
index: 1 value: 2
index: 2 value: 3
index: 3 value: 4
index: 4 value: 5
'''
- 直接迭代。
lst = [1, 2, 3, 4, 5]
for v in lst:
print(v)
'''
1
2
3
4
5
'''
- 使用 enumerate,以 index、value 迭代。
lst = [1, 2, 3, 4, 5]
for i, v in enumerate(lst):
print('index:', i, 'value:', v)
'''
index: 0 value: 1
index: 1 value: 2
index: 2 value: 3
index: 3 value: 4
index: 4 value: 5
'''
以上,
若有更好的想法歡迎提出哦!
留言
張貼留言