Hướng dẫn Python Lists: tạo list, truy cập item, xóa item, xóa list, và các thao tác khác.
Python List tạo bằng ngoặc vuông [], tách item bằng dấu phẩy ,.
List có thể chứa mixed types:
mixedTypesList = ['a', True, 1, 1.0]
Tạo List
colorsList = ["red", "green", "blue"]
print(colorsList) # ['red', 'green', 'blue']
Truy cập Item của List
Nhớ: item đầu tiên ở index 0.
colorsList = ["red", "green", "blue"]
print(colorsList[2]) # blue
Slicing (Cắt phạm vi)
Chỉ định start:end (end exclusive).
colorsList = ["red", "green", "blue", "orange", "yellow", "white"]
print(colorsList[1:4]) # ['green', 'blue', 'orange']
Negative Indexing
-1 = item cuối, -2 = item gần cuối.
colorsList = ["red", "green", "blue", "orange"]
print(colorsList[-1]) # orange
Thay đổi giá trị Item
colorsList = ["red", "green", "blue", "orange"]
colorsList[3] = "yellow"
print(colorsList) # ['red', 'green', 'blue', 'yellow']
Duyệt qua List
Dùng for loop:
colorsList = ["red", "green", "blue", "orange"]
for i in colorsList:
print(i)
# red, green, blue, orange
Thêm Item vào List
Hai method: append() (thêm cuối) và insert() (thêm tại index).
# append
colorsList = ["red", "green", "blue", "orange"]
colorsList.append("yellow")
print(colorsList) # ['red', 'green', 'blue', 'orange', 'yellow']
# insert
colorsList = ["red", "green", "blue", "orange"]
colorsList.insert(2, "yellow")
print(colorsList) # ['red', 'green', 'yellow', 'blue', 'orange']
Xóa Item khỏi List
Nhiều cách xóa:
remove(value): xóa item đầu tiên có giá trị chỉ địnhpop(index): xóa item tại index (mặc định index cuối)del list[index]: xóa item tại index hoặc xóa cả listclear(): làm rỗng list
# remove
colorsList = ["red", "green", "blue", "orange"]
colorsList.remove("orange")
print(colorsList) # ['red', 'green', 'blue']
# pop(index)
colorsList = ["red", "green", "blue", "orange"]
colorsList.pop(1)
print(colorsList) # ['red', 'blue', 'orange']
# pop() - xóa item cuối
colorsList = ["red", "green", "blue", "orange"]
colorsList.pop()
print(colorsList) # ['red', 'green', 'blue']
# del index
colorsList = ["red", "green", "blue", "orange"]
del colorsList[1]
print(colorsList) # ['red', 'blue', 'orange']
# del list - xóa hoàn toàn
colorsList = ["red", "green", "blue", "orange"]
del colorsList
print(colorsList) # NameError
# clear
colorsList = ["red", "green", "blue", "orange"]
colorsList.clear()
print(colorsList) # []
Độ dài List
Dùng len():
colorsList = ["red", "green", "blue", "orange"]
print(len(colorsList)) # 4
Đếm số lần xuất hiện Item
Dùng count():
colorsList = ["red", "green", "red", "orange"]
print(colorsList.count("red")) # 2
Sắp xếp List
sort() sắp xếp alphabetically (in-place):
colorsList = ["red", "green", "blue", "orange"]
colorsList.sort()
print(colorsList) # ['blue', 'green', 'orange', 'red']
Sắp xếp ngược (Reverse)
colorsList = ["red", "green", "blue", "orange"]
colorsList.sort(reverse=True)
print(colorsList) # ['red', 'orange', 'green', 'blue']
Đảo ngược List
Dùng reverse():
colorsList = ["red", "green", "blue", "orange"]
colorsList.reverse()
print(colorsList) # ['orange', 'blue', 'green', 'red']
Copy List sang List khác
Dùng copy():
colorsList = ["red", "green", "blue", "orange"]
newList = colorsList.copy()
print(newList) # ['red', 'green', 'blue', 'orange']
Nối hai List
Dùng toán tử +:
colorsList = ["red", "green", "blue", "orange"]
numbersList = [1, 2, 3, 4]
numbersAndColors = colorsList + numbersList
print(numbersAndColors)
# ['red', 'green', 'blue', 'orange', 1, 2, 3, 4]