Trong Python kết quả của hàm list enumerate 2 3 là gì

Ở bài Cấu trúc dữ liệu List trong Python, các bạn đã tìm hiểu về List. Trong bài này, chúng ta cùng xem xét một số thao tác có thể thực hiện trên List trong Python.

my_list = [2, 'goc', 'hoc', 'it', 8, 1, 3, 5] # Using for loop for i in my_list: print[i, end=' '] Kết quả2 goc hoc it 8 1 3 5my_list = [2, 'goc', 'hoc', 'it', 8, 1, 3, 5] # getting length of my_list length = len[my_list] # Iterating the index for i in range[length]: print[my_list[i], end=' '] Kết quả2 goc hoc it 8 1 3 5my_list = [2, 'goc', 'hoc', 'it', 8, 1, 3, 5] # getting length of my_list length = len[my_list] i = 0 # Iterating using while loop while i < length: print[my_list[i], end=' '] i += 1 Kết quả2 goc hoc it 8 1 3 5

Trả về một đối tượng bao gồm tất cả index và giá trị các phần tử tương ứng trong List.

my_list = [2, 'goc', 'hoc', 'it', 8, 1, 3, 5] # Using enumerate[] for i, val in enumerate[my_list]: print ['index ', i, " - value ", val, sep='']

Kết quảindex 0 - value 2 index 1 - value goc index 2 - value hoc index 3 - value it index 4 - value 8 index 5 - value 1 index 6 - value 3 index 7 - value 5

List comprehension cũng cấp một cú pháp ngắn gọn hơn để tạo một List mới dựa trên các giá trị của một List khác.

my_list = [2, 'goc', 'hoc', 'it', 8, 1, 3, 5] #Creating new_my_list include items which are string in my_list new_my_list = [x for x in my_list if type[x]==str] print[new_my_list]

Kết quả['goc', 'hoc', 'it']

Cú pháp của List Comprehension

new_my_list = [expression for item in iterable if condition == True]

Ví dụ về expression của List Comprehension

Expression là phần tử đang xử lý của List mới chuẩn bị được tạo ra.

fruits = ["apple", "banana", "cherry", "kiwi", "mango"] # Set the values in newlist to upper case newlist = [x.upper[] for x in fruits] print["newlist with upper case:", newlist] # Set all values in newlist to 'hello' newlist = ['hello' for x in fruits] print["newlist with 'hello':", newlist]

Kết quảnewlist with upper case: ['APPLE', 'BANANA', 'CHERRY', 'KIWI', 'MANGO'] newlist with 'hello': ['hello', 'hello', 'hello', 'hello', 'hello']

Ví dụ về iterable của List Comprehension

Iterable có thể là bất kỳ một iterable object nào như list, tuple, set,… Ví dụ, chúng ta có thể sử dụng hàm range[] để tạo ra một iterable object.

fruits = ["apple", "banana", "cherry", "kiwi", "mango"] # Creating newlist with range[] newlist = [x for x in range[10]] print["newlist in range[10]:", newlist] # Creating newlist accept only numbers lower than 5 newlist = [x for x in range[10] if x < 5] print["newlist in range[10] with condition:", newlist]

Kết quảnewlist in range[10]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] newlist in range[10] with condition: [0, 1, 2, 3, 4]

Ví dụ về condition của List Comprehension

Trong List Comprehension, condition là tùy chọn, có thể có hoặc không.

fruits = ["apple", "banana", "cherry", "kiwi", "mango"] #Condition with if # newlist include items which arenot 'apple' newlist = [x for x in fruits if x != "apple"] print["newlist with condition:", newlist] #Condition is optional, can be omitted # No if statement newlist = [x for x in fruits] print["newlist with no condition:", newlist]

Kết quảnewlist with condition: ['banana', 'cherry', 'kiwi', 'mango'] newlist with no condition: ['apple', 'banana', 'cherry', 'kiwi', 'mango']

Đối tượng List có hàm sort[] để sắp xếp các phần tử string và number trong List theo thứ tự giảm dần, tăng dần. Mặc định, các phần tử được sắp xếp theo thứ tự tăng dần. Nếu truyền đối số reverse = True cho hàm sort[] thì sẽ sắp xếp theo thứ tự giảm dần. Ví dụ:

char_list = ["banana", "apple", "cherry", "mango", "kiwi"] num_list = [100, 50, 65, 82, 23] # Sort by default char_list.sort[] num_list.sort[] print["char_list_after_default_sort:", char_list] print["num_list_after_default_sort:", num_list] # Sort by reverse = True char_list.sort[reverse = True] num_list.sort[reverse = True] print["char_list_after_reverse_sort:", char_list] print["num_list_after_reverse_sort:", num_list]

Kết quảchar_list_after_default_sort: ['apple', 'banana', 'cherry', 'kiwi', 'mango'] num_list_after_default_sort: [23, 50, 65, 82, 100] char_list_after_reverse_sort: ['mango', 'kiwi', 'cherry', 'banana', 'apple'] num_list_after_reverse_sort: [100, 82, 65, 50, 23]

Chúng ta có thể sử dụng hàm copy[] hoặc hàm list[] để sao chép một List. Ví dụ:

my_list = [2, 'goc', 'hoc', 'it', 8, 1, 3, 5] # copy[] function my_list_1 = my_list.copy[] print["my_list_1:", my_list_1] # list[] function my_list_2 = list[my_list] print["my_list_2:", my_list_2]

Kết quảmy_list_1: [2, 'goc', 'hoc', 'it', 8, 1, 3, 5] my_list_2: [2, 'goc', 'hoc', 'it', 8, 1, 3, 5]

Lưu ý: Chúng ta không thể dùng toán tử gán để sao chép 2 List như my_list_1 = my_list. Bởi vì my_list_1 chỉ tham chiếu đến List mà my_list tham chiếu đến. Lúc này, my_list_1 hoặc my_list thay đổi thì cũng sẽ dấn đến list kia thay đổi theo. Ví dụ:

my_list = [2, 'goc', 'hoc', 'it', 8, 1, 3, 5] my_list_1 = my_list # Change items in my_list_1 my_list_1[0] = 'hello all' my_list_1[1] = '9999' print["my_list changed after my_list_1 changed:", my_list]

Kết quảmy_list changed after my_list_1 changed: ['hello all', '9999', 'hoc', 'it', 8, 1, 3, 5]

Cách đơn giản nhất để nối [join] 2 hoặc nhiều List là sử dụng toán tử +.

my_list_1 = ['goc', 'hoc', 'it'] my_list_2 = [2, 2, 5, 8] my_list_3 = ['welcome', '99', 0, 1] # Concatenate my_list_1, my_list_2, my_list_3 my_list = my_list_1 + my_list_2 + my_list_3 print["my_list:", my_list]

Kết quảmy_list: ['goc', 'hoc', 'it', 2, 2, 5, 8, 'welcome', '99', 0, 1]

Chúng ta có thể sử dụng hàm append[] để nối 2 List với vòng lặp for.

my_list_1 = ['goc', 'hoc', 'it'] my_list_2 = [2, 2, 5, 8] # Concatenate my_list_1, my_list_2 with append[] and for for x in my_list_2: my_list_1.append[x] print["my_list_1:", my_list_1]

Kết quảmy_list_1: ['goc', 'hoc', 'it', 2, 2, 5, 8]

Chúng ta cũng có thể sử dụng hàm extend[] để nối một List với cuối List khác.

my_list_1 = ['goc', 'hoc', 'it'] my_list_2 = [2, 2, 5, 8] # Concatenate my_list_1, my_list_2 with extend[] my_list_1.extend[my_list_2] print["my_list_1:", my_list_1]

Kết quảmy_list_1: ['goc', 'hoc', 'it', 2, 2, 5, 8]

Python hỗ trợ nhiều method để giúp thao tác với List dễ dàng hơn. Một số method thường sử dụng với List được mô tả ở bảng bên dưới.

Phương thứcChức năng
append[]Thêm một phần tử vào cuối list
extend[]Thêm tất cả phần tử của một list vào cuối một list khác
insert[]Chèn một phần tử vào một vị trí được xác định trước
remove[]Xóa một phần tử ra khỏi list
pop[]Xóa và trả về một phần tử tại một index xác định trước [mặc định là xóa phần tử cuối list]
clear[]Xóa tất cả phần tử trong list
index[]Trả về index đầu tiên của phần tử được tìm thấy trong list
count[]Trả về số lượng phần tử được tìm thấy trong list
sort[]Sắp xếp các phần tử trong list
reverse[]Đảo thứ tự của các phần tử trong list
copy[]Copy một list

Ví dụ sử dụng các method của list

my_list = [2, 'goc', 'hoc', 'it', 8, 1, 3, 5] # Add 'welcome' to the end of my_list my_list.append['welcome'] # Output: [2, 'goc', 'hoc', 'it', 8, 1, 3, 5, 'welcome'] print["Append my_list:", my_list] my_list_extend = ['hello', 'John'] # Extend my_list by my_list_extend my_list.extend[my_list_extend] # Output: [2, 'goc', 'hoc', 'it', 8, 1, 3, 5, 'welcome', 'hello', 'John'] print["Extend my_list:", my_list] # Insert an item to my_list my_list.insert[1, 'gochocit.com'] # Output: [2, 'gochocit.com', 'goc', 'hoc', 'it', 8, 1, 3, 5, 'welcome', 'hello', 'John'] print["Insert my_list:", my_list] # Remove items from my_list my_list.remove[2] my_list.remove[8] my_list.remove[5] my_list.remove['welcome'] # Output: ['gochocit.com', 'goc', 'hoc', 'it', 1, 3, 'hello', 'John'] print["Remove my_list:", my_list] # Pop item by default from my_list pop_item = my_list.pop[] # Output: John print["pop item:", pop_item] # Output: ['gochocit.com', 'goc', 'hoc', 'it', 1, 3, 'hello'] print["Pop my_list:", my_list] # Pop item at index 3 from my_list pop_item = my_list.pop[3] # it print["pop item:", pop_item] # Output: ['gochocit.com', 'goc', 'hoc', 1, 3, 'hello'] print["Pop my_list:", my_list] # Index item from my_list index_item = my_list.index['hoc'] # Output: 2 print["Index item:", index_item] # Count my_list # Output: 1 print["Size of my_list:", my_list.count['gochocit.com']] # Clear my_list my_list.clear[] # Output: [] print["my_list:", my_list] Kết quảAppend my_list: [2, 'goc', 'hoc', 'it', 8, 1, 3, 5, 'welcome'] Extend my_list: [2, 'goc', 'hoc', 'it', 8, 1, 3, 5, 'welcome', 'hello', 'John'] Insert my_list: [2, 'gochocit.com', 'goc', 'hoc', 'it', 8, 1, 3, 5, 'welcome', 'hello', 'John'] Remove my_list: ['gochocit.com', 'goc', 'hoc', 'it', 1, 3, 'hello', 'John'] pop item: John Pop my_list: ['gochocit.com', 'goc', 'hoc', 'it', 1, 3, 'hello'] pop item: it Pop my_list: ['gochocit.com', 'goc', 'hoc', 1, 3, 'hello'] Index item: 2 Size of my_list: 1 my_list: []

Các bạn có thể tham khảo kỹ hơn các phương thức của List trong Python tại More on Lists.

Bài trước và bài sau trong môn học

Video liên quan

Chủ Đề