1. Giới thiệu
List là một cấu trúc dữ liệu cho phép lưu trữ nhiều kiểu dữ liệu khác nhau. List được sử dụng rộng rãi trong hầu hết các ngôn ngữ trong đó có python.
Các phần tử của list được đặt trong dấu ngoặc vuông.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = [1, 2, 3] | |
hoặc | |
l = list([1, 2, 3]) |
a. Chiều dài list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = [1, 2, 3] | |
print(len(l)) #3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = ['Practice', 'Machine', 'Learning'] | |
print(l[0]) # Practice | |
print(l[1]) # Machine | |
# Hay | |
print(l[-1]) # Learning | |
print(l[-2]) # Machine |
Để cập nhật phần tử của list ta truy cập đến phần tử đó và thay đổi giá trị
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = ['Practice', 'Machine', 'Learning'] | |
l[1] = 'Deep' # Machine -> Deep | |
print(l) # ['Practice', 'Deep', 'Learning'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = ['Practice', 'Machine', 'Learning'] | |
del l[0] | |
print(l) # ['Machine', 'Learning'] |
Phương thức này thêm phần tử vào cuối list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = ['Practice', 'Machine', 'Learning'] | |
l.append('!!!') | |
print(l) # ['Practice', 'Machine', 'Learning', '!!!'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Thêm các phần tử list 2 vào trong list 1 | |
list1.extend(list2) | |
l1 = ['I', 'Practice'] | |
l2 = ['Machine', 'Learning'] | |
l1.extend(l2) | |
print(l1) # ['I', Practice', 'Machine', 'Learning', '!!!'] |
Nếu các phần tử là chuỗi thì so sánh ký tự đầu tiên của mỗi chuỗi theo alphabet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l1 = [1, 2, 3] | |
print(min(l1)) #1 | |
print(max(l1)) #3 | |
l2 = ['Machine', 'Learning'] | |
print(min(l2)) # Learning | |
print(max(l2)) # Machine |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = ['A', 'B', 'A', 'C'] | |
print(l.count('A')) # 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = ['Machine', 'Learning'] | |
l.insert(1, '...') # chèn chuỗi '...' vào vị trí số 1 | |
print(l) # ['Machine', '...', 'Learning'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = ['Machine', 'Learning'] | |
l.reverse() | |
print(l) # ['Learning', 'Machine'] | |
# Hoặc sử dụng l[::-1] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = ['Machine', 'Learning'] | |
l.clear() | |
print(l) # [] |
0 nhận xét:
Đăng nhận xét