HƯỚNG dẫn sử DỤNG LIST TRONG PYTHON

5 127 0
HƯỚNG dẫn sử DỤNG LIST TRONG PYTHON

Đang tải... (xem toàn văn)

Thông tin tài liệu

HƯỚNG DẪN SỬ DỤNG LIST TRONG PYTHON 1 PYTHON LIST Trong Python, List là một kiểu dữ liệu linh hoạt nhất. Nó là dẫy (sequence) phần tử (element), nó cho phép loại bỏ, hoặc thêm các phần tử vào danh sách, đồng thời cho phép cắt lát (Slice) các phần tử. Để viết một danh sách, bạn đặt các phần tử nằm trong cặp ngoặc vuông và ngăn cách nhau bởi dấu phẩy. Các phần tử trong danh sách được đánh chỉ số (index) từ trái sang phải bắt đầu từ chỉ số 0. listExample.py ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 fruitList = apple, apricot, banana,coconut, lemen otherList = 100, one, two, 3 print (Fruit List:) print (fruitList) print ( ) print (Other List:) print (otherList) 2 TRUY CẬP CÁC PHẦN TỬ CỦA DANH SÁCH   3 CẬP NHẬP DANH SÁCH Ví dụ dưới đây là cách cập nhật List theo chỉ số (index): updateListExample.py ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 years = 1991,1995, 1992 print (Years: , years) print (Set years1 = 2000) years1 = 2000 print (Years: , years) print ( years ) print (Append element 2015, 2016 to list) Nối (append) thêm một phần tử vào cuối danh sách. years.append( 2015 ) years.append( 2016 ) print (Years: , years) Bạn cũng có thể cập nhập giá trị cho một lát (Slice) các phần tử. Đây là cách để bạn cập nhập nhiều phần tử một lúc. Một lát (Slice) là một vài phần tử có vị trí liên tiếp nhau trong một danh sách. sliceUpdateExample.py ? 1 2 3 4 5 6 7 8 9 10 11 years = 1990 , 1991 , 1992 , 1993 , 1994 , 1995 , 1996 print (Years: , years) print (Update Slice: years1:5 = 2000, 2001) years1:5 = 2000 , 2001 print (Years: , years) 4 XÓA PHẦN TỬ TRONG DANH SÁCH Để xóa một hoặc nhiều phần tử trong một danh sách (list) bạn có thể sử dụng lệnh del (del statement), hoặc sử dụng phương thức remove(). Ví dụ dưới đây sử dụng lệnh del để xóa một hoặc nhiều phần tử theo chỉ số. deleteElementExample.py ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 years = 1990 , 1991 , 1992 , 1993 , 1994 , 1995 , 1996 print (Years: , years) print ( del years6) Xóa phần tử tại vị trí có index = 6. del years6 print (Years: , years) print ( del years1:4) Xóa phần tử tại index = 1,2,3 del years1:4 print (Years: , years) Truy cập các phần tử Sử dụng vòng lặp for để truy cập vào các phần tử của danh sách: accessElementExample.py ? 1 2 3 4 5 6 fruitList = apple, apricot, banana, coconut, lemen, plum, pear for fruit in fruitList : print (Fruit: , fruit) Truy cập thông qua chỉ số (index): indexAccessExample.py ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 fruitList = apple, apricot, banana, coconut, lemen, plum, pear print ( fruitList ) Số phần tử. print (Element count: , len(fruitList) ) for i in range (0, len(fruitList) ) : print (Element at , i, = , fruitListi ) Một danh sách con chứa các phần tử từ index 1 đến 4 (1, 2, 3) subList = fruitList1: 4 apricot, banana, coconut print (Sub List 1:4 , subList ) Bạn cũng có thể truy cập vào các phần tử của danh sách theo chỉ số âm (Negative index), các phần tử được đánh chỉ số từ phải sang trái với các giá trị 1, 2, ... indexAccessExample2.py ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 fruitList = apple, apricot, banana, coconut, lemen, plum, pear print ( fruitList ) print (Element count: , len(fruitList) ) print (fruitList1: , fruitList1) print (fruitList2: , fruitList2) subList1 = fruitList4: print ( ) print (Sub List fruitList4: ) print (subList1) subList2 = fruitList4:2 print ( ) print (Sub List fruitList4:2 ) print (subList2)

HƯỚNG DẪN SỬ DỤNG LIST TRONG PYTHON 1- PYTHON LIST Trong Python, List kiểu liệu linh hoạt Nó dẫy (sequence) phần tử (element), cho phép loại bỏ, thêm phần tử vào danh sách, đồng thời cho phép cắt lát (Slice) phần tử Để viết danh sách, bạn đặt phần tử nằm cặp ngoặc vuông [ ] ngăn cách dấu phẩy Các phần tử danh sách đánh số (index) từ trái sang phải số listExample.py ? 10 11 12 13 14 15 16 fruitList = ["apple", "apricot", "banana","coconut", "lemen"] otherList = [100, "one", "two", 3] print ("Fruit List:") print (fruitList) print (" - ") print ("Other List:") print (otherList) 2- TRUY CẬP CÁC PHẦN TỬ CỦA DANH SÁCH 3- CẬP NHẬP DANH SÁCH Ví dụ cách cập nhật List theo số (index): updateListExample.py ? 10 11 12 13 14 15 16 17 18 19 20 21 22 years = [1991,1995, 1992] print ("Years: ", years) print ("Set years[1] = 2000") years[1] = 2000 print ("Years: ", years) print ( years ) print ("Append element 2015, 2016 to list") # Nối (append) thêm phần tử vào cuối danh sách years.append( 2015 ) years.append( 2016 ) print ("Years: ", years) Bạn cập nhập giá trị cho lát (Slice) phần tử Đây cách để bạn cập nhập nhiều phần tử lúc Một lát (Slice) vài phần tử có vị trí liên tiếp danh sách sliceUpdateExample.py ? 10 11 years = [ 1990 , 1991 , 1992 , 1993 , 1994 , 1995 , print ("Years: ", years) print ("Update Slice: years[1:5] = [2000, 2001]") years[1:5] = [ 2000 , 2001 ] print ("Years: ", years) 1996 ] 4- XÓA PHẦN TỬ TRONG DANH SÁCH Để xóa nhiều phần tử danh sách (list) bạn sử dụng lệnh del (del statement), sử dụng phương thức remove() Ví dụ sử dụng lệnh del để xóa nhiều phần tử theo số deleteElementExample.py ? 10 11 12 13 14 15 16 17 18 19 years = [ 1990 , 1991 , 1992 , 1993 , 1994 , 1995 , print ("Years: ", years) print ("\n del years[6]") # Xóa phần tử vị trí có index = del years[6] print ("Years: ", years) print ("\n del years[1:4]") # Xóa phần tử index = 1,2,3 del years[1:4] print ("Years: ", years) Truy cập phần tử Sử dụng vòng lặp for để truy cập vào phần tử danh sách: accessElementExample.py ? fruitList = ["apple", "apricot", "banana", "coconut", "lemen", "plum", "pear"] for fruit in fruitList : print ("Fruit: ", fruit) 1996 ] Truy cập thông qua số (index): indexAccessExample.py ? 10 11 12 13 14 15 16 17 fruitList = ["apple", "apricot", "banana", "coconut", "lemen", "plum", "pear"] print ( fruitList ) # Số phần tử print ("Element count: ", len(fruitList) ) for i in range (0, len(fruitList) ) : print ("Element at ", i, "= ", fruitList[i] ) # Một danh sách chứa phần tử từ index đến (1, 2, 3) subList = fruitList[1: 4] # ['apricot', 'banana', 'coconut'] print ("Sub List [1:4] ", subList ) Bạn truy cập vào phần tử danh sách theo số âm (Negative index), phần tử đánh số từ phải sang trái với giá trị -1, -2, indexAccessExample2.py ? 10 11 12 13 14 15 16 17 fruitList = ["apple", "apricot", "banana", "coconut", "lemen", "plum", "pear"] print ( fruitList ) print ("Element count: ", len(fruitList) ) print ("fruitList[-1]: ", fruitList[-1]) print ("fruitList[-2]: ", fruitList[-2]) subList1 = fruitList[-4: ] print ("\n") print ("Sub List fruitList[-4: ] ") print (subList1) 18 19 20 subList2 = fruitList[-4:-2] 21 22 print ("\n") 23 print ("Sub List fruitList[-4:-2] ") print (subList2) ... fruitList[-1]) print ("fruitList[-2]: ", fruitList[-2]) subList1 = fruitList[-4: ] print (" ") print ("Sub List fruitList[-4: ] ") print (subList1) 18 19 20 subList2 = fruitList[-4:-2] 21 22 print... ", years) 1996 ] 4- XĨA PHẦN TỬ TRONG DANH SÁCH Để xóa nhiều phần tử danh sách (list) bạn sử dụng lệnh del (del statement), sử dụng phương thức remove() Ví dụ sử dụng lệnh del để xóa nhiều phần... 16 17 fruitList = ["apple", "apricot", "banana", "coconut", "lemen", "plum", "pear"] print ( fruitList ) print ("Element count: ", len(fruitList) ) print ("fruitList[-1]: ", fruitList[-1]) print

Ngày đăng: 27/08/2019, 16:41

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan