Kiểu list trong Python
List sử dụng để lưu một lúc nhiều giá trị trong một biến. List là một trong 4 kiểu lưu trữ dữ liệu dạng tập hợp trong Python, 3 loại còn lại là Tupe, Dictionary và Set. 4 loại này khác nhau về cách sử dụng. Hôm nay chúng ta cùng nhau đi tìm hiểu về Python List.
Nội dung của bài
List trong Python
fruits = ["apple", "banana", "cherry"]
for m in fruits:
print(fruits[m])
if fruits[m] == "apple":
fruits[m] = "Apple"
List là một tập hợp đã được sắp xếp tuần tự và không thể thay đổi vị trí. Khi chúng ta thêm mới một phần tử vào list thì nó sẽ được thêm vào cuối của danh sách. Giá trị của mỗi phần tử trong list có thể thay đổi và có thể trùng nhau.
List được index, phần tử đầu tiên có index là 0 tiếp theo là 1… các phần tử của list có thể có các kiểu khác nhau.
Truy xuất vào phần tử của List
Như trong ví dụ trên, chúng ta có một list là fruits, vậy để lấy ra phần tử có giá trị là “apple” chúng ta sử dụng cú pháp: fruits[0], vì phần tử này là phần tử đầu tiên của list nên có index là 0. Index có thể là giá trị âm, ví dụ muốn lấy ra phần tử cuối cùng của list trên chúng ta có thể sử dụng fruits[2] hoặc fruits[-1].
Chúng ta cũng có thể lấy ra môt danh sách con từ danh sách cha:
fruits = ["apple", "banana", "cherry", "orange"]
fruits1 = fruits[1:3]
Ví dụ trên danh sách fruits1 sẽ có giá trị: [“banana”, “cherry”, “orange”]
Cập nhật danh sách
fruits = ["apple", "banana", "cherry", "orange"]
# cập nhật danh sách
fruits[0] = "longan"
print(fruits)
# prints ["longan", "banana", "cherry", "orange"]
# xóa phần tử trong danh sách
del fruits[0]
print(fruits)
# prints ["banana", "cherry", "orange"]
# thêm mới phần tử
fruits += ["apple"]
print(fruits)
# prints ["banana", "cherry", "orange", "apple"]
del fruits[0]
fruits.append("banana")
print(fruits)
# prints ["cherry", "orange", "apple", "banana"]
fruits.extend(["rose apple", "passionfruit"])
print(fruits)
# prints ["cherry", "orange", "apple", "banana", "rose apple", "passionfruit"]
Từ ví dụ trên chúng ta có thể hiểu được cách thức cập nhật, xóa, thêm mới phần tử vào danh sách. Có điểm chú ý là dùng append() chúng ta chỉ thêm được một phần tử còn extend() chúng ta có thể thêm vào danh sách nhiều phần tử một lúc.
Một số phương thức hay dùng trong list
Ngoài phương thức append() và extend(), chúng ta tìm hiểu thêm một số phương thức trong ví dụ dưới đây
fruits = ["apple", "banana", "cherry", "orange"]
# loại bỏ phần tử có giá trị banana
fruits.remove("banana")
# xác định index của một phần tử
fruits.index("cherry") # trả ra kết quả là 1, vì lúc này danh sách là ["banana", "cherry", "orange"]
# đảo ngược các phần tử của danh sách
fruits.reverse() # danh sách có giá trị mới là ["orange", "cherry", "banana"]
# sắp xếp lại các phần tử
fruits.sort() # danh sách có giá trị mới là ["banana", "cherry", "orange"]
Kết luận
Hôm nay chúng ta đã tìm hiểu về danh sách trong Python, đây là một trong 4 kiểu tập hợp trong Python. List rất linh hoạt và hỗ trợ rất nhiều phương thức có sẵn. Bạn hãy thực hành sử dụng list theo hướng dẫn nhé.
[…] đã nói ở phần danh sách trong Python (Python List) có 4 loại dữ liệu tập hợp ( lưu trữ nhiều giá trị trong một biến). Tupe […]