Tuple trong Python

Tuple trong Python

Phan Nhật Chánh

Chánh27 tháng 09, 2020

5 min read
·
views
·
likes

Tuple là một dạng kiểu dữ liệu dùng để lưu trữ các đối tượng không thay đổi về sau (giống kiểu hằng số) và cách lưu trữ và sử dụng của nó cũng khá giống như kiểu dữ liệu List. Ở đây ta cần lưu ý là các phần tử của Tuple thì nằm trong cặp ngoặc (), cách nhau bởi dấu phẩy , và được đánh chỉ số bắt đầu từ chỉ số 0.

Tạo một Tuple

  • Tuple rỗng: Tạo một tuple trống
# cú pháp void_tuple = () # hoặc sử dụng hàm tạo tuple void_tuple = tuple ()
# cú pháp void_tuple = () # hoặc sử dụng hàm tạo tuple void_tuple = tuple ()
  • Tuple với các giá trị
tpl = ('item1', 'item2','item3') fruits = ('banana', 'orange', 'mango', 'lemon')
tpl = ('item1', 'item2','item3') fruits = ('banana', 'orange', 'mango', 'lemon')

Lấy độ dài Tuple

Chúng ta sử dụng phương thức len() để lấy độ dài của một tuple.

tpl = ('item1','item2','item3') len(tpl) # 3
tpl = ('item1','item2','item3') len(tpl) # 3

Truy cập các phần tử Tuble

Ví dụ: Truy cập các phần tử của Tuple

Day = ('Thứ 2', 'Thứ 3', 'Thứ 4' , 'Thứ 5', 'Thứ 6', 'Thứ 7' , 'Chủ nhật') print (Day[0]) #KQ: Thứ 2 print (Day[2]) #KQ: Thứ 4
Day = ('Thứ 2', 'Thứ 3', 'Thứ 4' , 'Thứ 5', 'Thứ 6', 'Thứ 7' , 'Chủ nhật') print (Day[0]) #KQ: Thứ 2 print (Day[2]) #KQ: Thứ 4

Nếu bạn muốn tách ra một Tuple con của Tuple hiện tại thì bạn có thể sử dụng cú pháp tupleName[start:end] (nó giống với cách truy cập các phần tử của list)

Day = ('Thứ 2', 'Thứ 3', 'Thứ 4' , 'Thứ 5', 'Thứ 6', 'Thứ 7' , 'Chủ nhật') print (Day[1:3]) #KQ: ('Thứ 3', 'Thứ 4') print (Day[:3]) #KQ: ('Thứ 2', 'Thứ 3', 'Thứ 4') print (Day[3:]) #KQ: ('Thứ 5', 'Thứ 6', 'Thứ 7', 'Chủ nhật') print (Day[-3]) # Lấy 1 phần tử từ phải qua trái. KQ: Thứ 6
Day = ('Thứ 2', 'Thứ 3', 'Thứ 4' , 'Thứ 5', 'Thứ 6', 'Thứ 7' , 'Chủ nhật') print (Day[1:3]) #KQ: ('Thứ 3', 'Thứ 4') print (Day[:3]) #KQ: ('Thứ 2', 'Thứ 3', 'Thứ 4') print (Day[3:]) #KQ: ('Thứ 5', 'Thứ 6', 'Thứ 7', 'Chủ nhật') print (Day[-3]) # Lấy 1 phần tử từ phải qua trái. KQ: Thứ 6
Truy cập các phần tử của Tuple
tpl = ('item1', 'item2', 'item3') first_item = tpl[0] second_item = tpl[1] fruits = ('banana', 'orange', 'mango', 'lemon') first_fruit = fruits[0] # banana second_fruit = fruits[1] # orange last_index =len(fruits) - 1 # 3 last_fruit = fruits[last_index] # lemon
tpl = ('item1', 'item2', 'item3') first_item = tpl[0] second_item = tpl[1] fruits = ('banana', 'orange', 'mango', 'lemon') first_fruit = fruits[0] # banana second_fruit = fruits[1] # orange last_index =len(fruits) - 1 # 3 last_fruit = fruits[last_index] # lemon
Truy cập các phần tử của Tuple
tpl = ('item1', 'item2', 'item3','item4') first_item = tpl[-4] second_item = tpl[-3] fruits = ('banana', 'orange', 'mango', 'lemon') first_fruit = fruits[-4] second_fruit = fruits[-3] last_fruit = fruits[-1]
tpl = ('item1', 'item2', 'item3','item4') first_item = tpl[-4] second_item = tpl[-3] fruits = ('banana', 'orange', 'mango', 'lemon') first_fruit = fruits[-4] second_fruit = fruits[-3] last_fruit = fruits[-1]

Chia nhỏ Tuples

Chúng ta có thể chia nhỏ một tuple thành một tuple khác bằng cách chỉ định một dải các phần tử (startend), giá trị trả về sẽ là một tuple mới với các phần tử được chỉ định.

Ví dụ 1
tpl = ('item1', 'item2', 'item3','item4') all_items = tpl[0:4] # all items all_items = tpl[0:] # all items middle_two_items = tpl[1:3] # does not include item at index 3 fruits = ('banana', 'orange', 'mango', 'lemon') all_fruits = fruits[0:4] # all items all_fruits= fruits[0:] # all items orange_mango = fruits[1:3] # doesn't include item at index 3 orange_to_the_rest = fruits[1:]
Ví dụ 1
tpl = ('item1', 'item2', 'item3','item4') all_items = tpl[0:4] # all items all_items = tpl[0:] # all items middle_two_items = tpl[1:3] # does not include item at index 3 fruits = ('banana', 'orange', 'mango', 'lemon') all_fruits = fruits[0:4] # all items all_fruits= fruits[0:] # all items orange_mango = fruits[1:3] # doesn't include item at index 3 orange_to_the_rest = fruits[1:]
tpl = ('item1', 'item2', 'item3','item4') all_items = tpl[-4:] # all items middle_two_items = tpl[-3:-1] # does not include item at index 3 (-1) fruits = ('banana', 'orange', 'mango', 'lemon') all_fruits = fruits[-4:] # all items orange_mango = fruits[-3:-1] # doesn't include item at index 3 orange_to_the_rest = fruits[-3:]
tpl = ('item1', 'item2', 'item3','item4') all_items = tpl[-4:] # all items middle_two_items = tpl[-3:-1] # does not include item at index 3 (-1) fruits = ('banana', 'orange', 'mango', 'lemon') all_fruits = fruits[-4:] # all items orange_mango = fruits[-3:-1] # doesn't include item at index 3 orange_to_the_rest = fruits[-3:]

Tuples thành Lists

Chúng ta có thể thay đổi Tuples thành Lists và ngược lại (Lists thành Tuples).

tpl = ('item1', 'item2', 'item3','item4') lst = list(tpl) fruits = ('banana', 'orange', 'mango', 'lemon') fruits = list(fruits) fruits[0] = 'apple' print(fruits) # ['apple', 'orange', 'mango', 'lemon'] fruits = tuple(fruits) print(fruits) # ('apple', 'orange', 'mango', 'lemon')
tpl = ('item1', 'item2', 'item3','item4') lst = list(tpl) fruits = ('banana', 'orange', 'mango', 'lemon') fruits = list(fruits) fruits[0] = 'apple' print(fruits) # ['apple', 'orange', 'mango', 'lemon'] fruits = tuple(fruits) print(fruits) # ('apple', 'orange', 'mango', 'lemon')

Kiểm tra phần tử Tuples

Bạn có thể kiểm tra xem một phần tử trong tuble có tồn tại hay không bằng cách sử dụng in kết quả trả về là kiểu dữ liệu boolean

tpl = ('item1', 'item2', 'item3','item4') 'item2' in tpl # True fruits = ('banana', 'orange', 'mango', 'lemon') print('orange' in fruits) # True print('apple' in fruits) # False fruits[0] = 'apple' # TypeError: 'tuple' object does not support item assignment
tpl = ('item1', 'item2', 'item3','item4') 'item2' in tpl # True fruits = ('banana', 'orange', 'mango', 'lemon') print('orange' in fruits) # True print('apple' in fruits) # False fruits[0] = 'apple' # TypeError: 'tuple' object does not support item assignment

Join Tuples

Sử dụng toán tử + để hợp nhất hai hay nhiều tuple

tpl1 = ('item1', 'item2', 'item3') tpl2 = ('item4', 'item5','item6') tpl3 = tpl1 + tpl2 fruits = ('banana', 'orange', 'mango', 'lemon') vegetables = ('Tomato', 'Potato', 'Cabbage','Onion', 'Carrot') fruits_and_vegetables = fruits + vegetables
tpl1 = ('item1', 'item2', 'item3') tpl2 = ('item4', 'item5','item6') tpl3 = tpl1 + tpl2 fruits = ('banana', 'orange', 'mango', 'lemon') vegetables = ('Tomato', 'Potato', 'Cabbage','Onion', 'Carrot') fruits_and_vegetables = fruits + vegetables

Delete Tuples

Bạn không thể xóa phần tử được chỉ định. Nhưng bạn có thể xóa toàn bộ tuble.

tpl1 = ('item1', 'item2', 'item3') del tpl1 fruits = ('banana', 'orange', 'mango', 'lemon') del fruits
tpl1 = ('item1', 'item2', 'item3') del tpl1 fruits = ('banana', 'orange', 'mango', 'lemon') del fruits

Bài tập

  1. Tạo một tuples rỗng
  2. Tạo một tuples có chứa tên anh, chị, em của bạn (anh chị em trong tưởng tượng cũng được)
  3. Bạn tham gia vào tuples trên.
  4. Bạn có bao nhiêu anh chị em ruột?
  5. Sửa đổi bộ tuple của anh chị em và thêm tên của cha và mẹ của bạn và gán nó cho biến family_members
  6. Chia nhỏ từng thành viên trong gia đình bạn khỏi biến family_members
  7. Tạo 3 tuples từ trái cây, rau và động vật. Nối ba 3 tuples lại với nhau và gán nó vào một biến gọi là food_stuff_tp.
  8. Thay đổi bộ tuple food_stuff_tp thành list với tên food_stuff_lt
  9. Cắt bỏ phần tử ở giữa hoặc các phần tử khỏi list food_stuff_tp hoặc food_stuff_lt.
  10. Cắt ba phần tử đầu tiên và ba phần tử cuối cùng khỏi list food_staff_lt
  11. Xóa hoàn toàn bộ tuple food_staff_tp