Trong Python, Tuples [bộ dữ liệu] là một dẫy [sequence] các giá trị, nó chứa nhiều phần tử [element], về cơ bản nó khá giống với List [danh sách]. Nhưng khác với List, Tuples là một kiểu dữ liệu bất biến [immutable], mọi thao tác cập nhập trên Tuple đều tạo ra một thực thể mới trên bộ nhớ [memory].
Nội dung chính Để viết một Tuple, bạn đặt các phần tử nằm trong cặp ngoặc [ ] 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] bắt đầu từ chỉ số 0. tupleExample.py fruitTuple = ["apple", "apricot", "banana","coconut", "lemen"] otherTuple = [100, "one", "two", 3] print ["Fruit Tuple:"] print [fruitTuple] print [" --------------------------- "] print ["Other Tuple:"] print [otherTuple]Output: Fruit Tuple: ['apple', 'apricot', 'banana', 'coconut', 'lemen'] --------------------------- Other List: [100, 'one', 'two', 3] 2- So sánh List và Tuple
List và Tuple đều là một dẫy [sequence] các phần tử. Chúng có các khác biệt sau:
# Đây là một Tuple. aTuple = ["apple", "apricot", "banana"] # Đây là một List [Danh sách]. aList = ["apple", "apricot", "banana"]
listMemoryTest.py list1 = [1990, 1991, 1992] print ["list1: ", list1] # Địa chỉ của list1 trên bộ nhớ. list1Address = hex [ id[list1] ] print ["Address of list1: ", list1Address ] print ["\n"] print ["Append element 2001 to list1"] # Nối [append] một phần tử vào list1. list1.append[2001] print ["list1 [After append]: ", list1] # Địa chỉ của list1 trên bộ nhớ. list1Address = hex [ id[list1] ] print ["Address of list1 [After append]: ", list1Address ]
tupleMemoryTest.py tuple1 = [1990, 1991, 1992] # Địa chỉ của tuple1 trên bộ nhớ. tuple1Address = hex [ id[tuple1] ] print ["Address of tuple1: ", tuple1Address ] # Nối một tuple vào tuple1. tuple1 = tuple1 + [2001, 2002] # Địa chỉ của tuple1 trên bộ nhớ. tuple1Address = hex [ id[tuple1] ] print ["Address of tuple1 [After concat]: ", tuple1Address ]Output: Address of tuple1: 0x9096751d80 Address of tuple1 [After concat]: 0x9096778f10 3- Truy cập các phần tử của TuplesTruy 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 Tuple: elementAccessExample.py fruits = ["apple", "apricot", "banana", "coconut", "lemen", "plum", "pear"] for fruit in fruits : print ["Fruit: ", fruit]Output: Fruit: apple Fruit: apricot Fruit: banana Fruit: coconut Fruit: lemen Fruit: plum Fruit: pear Truy cập thông qua chỉ số [index]: Bạn cũng có thể truy cập vào các phần tử của Tuple thông qua chỉ số. Các phần tử của Tuple được đánh chỉ chỉ từ trái sang phải, bắt đầu từ 0. indexAccessExample.py fruits = ["apple", "apricot", "banana", "coconut", "lemen", "plum", "pear"] print [ fruits ] # Số phần tử. print ["Element count: ", len[fruits] ] for i in range [0, len[fruits] ] : print ["Element at ", i, "= ", fruits[i] ] # Một Tuple con chứa các phần tử từ index 1 đến 4 [1, 2, 3] subTuple = fruits[1: 4] # ['apricot', 'banana', 'coconut'] print ["Sub Tuple [1:4] ", subTuple ]Output: ['apple', 'apricot', 'banana', 'coconut', 'lemen', 'plum', 'pear'] Element count: 7 Element at 0 = apple Element at 1 = apricot Element at 2 = banana Element at 3 = coconut Element at 4 = lemen Element at 5 = plum Element at 6 = pear Sub Tuple [1:4] ['apricot', 'banana', 'coconut'] Bạn cũng có thể truy cập vào các phần tử của Tuple 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 fruits = ["apple", "apricot", "banana", "coconut", "lemen", "plum", "pear"] print [ fruits ] print ["Element count: ", len[fruits] ] print ["fruits[-1]: ", fruits[-1]] print ["fruits[-2]: ", fruits[-2]] subTuple1 = fruits[-4: ] print ["\n"] print ["Sub Tuple fruits[-4: ] "] print [subTuple1] subTuple2 = fruits[-4:-2] print ["\n"] print ["Sub Tuple fruits[-4:-2] "] print [subTuple2]Output: ['apple', 'apricot', 'banana', 'coconut', 'lemen', 'plum', 'pear'] Element count: 7 fruitList[-1]: pear fruitList[-2]: plum Sub Tuple fruits[-4: ] ['coconut', 'lemen', 'plum', 'pear'] Sub Tuple fruits[-4:-2] ['coconut', 'lemen'] 4- Cập nhập TuplesChú ý rằng Tuple là bất biến [immutable], vì vậy nó chỉ có các phương thức hoặc toán tử để truy cập, hoặc tạo ra một Tuple mới từ các Tuple ban đầu. updateTupleExample.py tuple1 = [1990, 1991, 1992] print ["tuple1: ", tuple1] print ["Concat [2001, 2002] to tuple1"] tuple2 = tuple1 + [2001, 2002] print ["tuple2: ", tuple2] # Một Tuple con, chứa các phần tử từ chỉ số 1 đến 4 [1,2,3] tuple3 = tuple2[1:4] print ["tuple2[1:4]: ", tuple3] # Một Tuple con, chứa các phần tử từ chỉ số 1 đến cuối. tuple4 = tuple2[1: ] print ["tuple2[1: ]: ", tuple4]Output: tuple1: [1990, 1991, 1992] Concat [2001, 2002] to tuple1 tuple2: [1990, 1991, 1992, 2001, 2002] tuple2[1:4]: [1991, 1992, 2001] tuple2[1: ]: [1991, 1992, 2001, 2002] 5- Các toán tử cơ bản cho TuplesCũng giống như String, Tuple có 3 toán tử +, * , in.
tupleOperatorsExample.py tuple1 = [1, 2, 3] tuple2 = ["One", "Two"] print ["tuple1: ", tuple1] print ["tuple2: ", tuple2] print ["\n"] tuple12 = tuple1 + tuple2 print ["tuple1 + tuple2: ", tuple12] tuple2x3 = tuple2 * 3 print ["tuple2 * 3: ", tuple2x3] hasThree = "Three" in tuple2 print ["'Three' in tuple2? ", hasThree]Output: tuple1: [1, 2, 3] tuple2: ['One', 'Two'] tuple1 + tuple2: [1, 2, 3, 'One', 'Two'] list2 * 3: ['One', 'Two', 'One', 'Two', 'One', 'Two'] 'Three' in tuple2? False 6- Các hàm liên quan TuplesHàmMô tả
tupleFunctionsExample.py tuple1 = [1991, 1994, 1992] tuple2 = [1991, 1994, 2000, 1992] print ["tuple1: ", tuple1] print ["tuple2: ", tuple2] # Trả về số lượng phần tử trong danh sách. print ["len[tuple1]: ", len[tuple1] ] print ["len[tuple2]: ", len[tuple2] ] # Giá trị lớn nhất trong Tuple. maxValue = max[tuple1] print ["Max value of tuple1: ", maxValue] # Giá trị nhỏ nhất trong Tuple. minValue = min[tuple1] print ["Min value of tuple1: ", minValue] # [all] # List list3 = [2001, 2005, 2012] print ["list3: ", list3] # Chuyển đổi một List thành tuple. tuple3 = tuple [list3] print ["tuple3: ", tuple3]Output: tuple1: [1991, 1994, 1992] tuple2: [1991, 1994, 2000, 1992] len[tuple1]: 3 len[tuple2]: 4 Max value of tuple1: 1994 Min value of tuple1: 1991 list3: [2001, 2005, 2012] tuple3: [2001, 2005, 2012]
7- Các phương thứcPhương thứcMô tả
Ví dụ: tupleMethodsExample.py years = [ 1990 , 1991 , 1993 , 1991 , 1993 , 1993 , 1993 ] print ["Years: ", years] print ["\n"] # Trả về số lần xuất hiện của 1993 print ["years.count[1993]: ",years.count[1993] ] # Tìm vị trí xuất hiện 1993 print ["years.index[1993]: ", years.index[1993] ] # Tìm vị trí xuất hiện 1993, bắt đầu từ chỉ số 3 print ["years.index[1993, 3]: ", years.index[1993, 3] ] # Tìm vị trí xuất hiện 1993, bắt đầu từ chỉ số 4 tới 5 [Không bao gồm 6] print ["years.index[1993, 4, 6]: ", years.index[1993, 4, 6] ]Output: Years: [1990, 1991, 1993, 1991, 1993, 1993, 1993] years.count[1993]: 4 years.index[1993]: 2 years.index[1993, 3]: 4 years.index[1993, 4, 6]: 4
Có thể bạn quan tâm Đây là các khóa học trực tuyến bên ngoài website o7planning mà chúng tôi giới thiệu, nó có thể bao gồm các khóa học miễn phí hoặc giảm giá. |