Khi cần nối nhiều bộ dữ liệu, toán tử '+' có thể được sử dụng. Tuple là một kiểu dữ liệu không thể thay đổi. Điều đó có nghĩa là, các giá trị một khi được xác định không thể thay đổi bằng cách truy cập các phần tử chỉ mục của chúng. Nếu chúng ta cố gắng thay đổi các phần tử, nó sẽ dẫn đến lỗi. Chúng rất quan trọng chứa vì chúng đảm bảo quyền truy cập chỉ đọc
Toán tử '+' có thể được sử dụng để thêm các giá trị số hoặc nối các chuỗi
Dưới đây là một minh chứng tương tự -
Ví dụ
Bản thử trực tiếp
my_tuple_1 = [11, 14, 0, 78, 33, 11] my_tuple_2 = [10, 78, 0, 56, 8, 34] print["The first tuple is : "] print[my_tuple_1] print["The second tuple is : "] print[my_tuple_2] my_result = my_tuple_1 + my_tuple_2 print["The tuple after concatenation is : " ] print[my_result]
đầu ra
The first tuple is : [11, 14, 0, 78, 33, 11] The second tuple is : [10, 78, 0, 56, 8, 34] The tuple after concatenation is : [11, 14, 0, 78, 33, 11, 10, 78, 0, 56, 8, 34]
Giải trình
- Hai bộ dữ liệu được xác định và được hiển thị trên bảng điều khiển
- Chúng được nối bằng toán tử '+'
- Điều này được gán cho một giá trị
- Nó được hiển thị trên bàn điều khiển
Đôi khi, trong khi làm việc với các bản ghi, chúng ta có thể gặp vấn đề chung là thêm nội dung của một bộ dữ liệu với chỉ mục tương ứng của bộ dữ liệu khác. Điều này có ứng dụng trong hầu hết các lĩnh vực mà chúng tôi làm việc với các bản ghi bộ. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.
Phương pháp số 1. Sử dụng map[] + lambda Kết hợp các chức năng trên có thể giải quyết vấn đề cho chúng tôi. Trong phần này, chúng tôi tính tổng bằng các hàm lambda và mở rộng logic cho các khóa bằng map[].
Python3
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]0
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]2
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]3
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]4
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]0
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]2
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]4
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]4
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]4
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]3
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]7
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]8
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]90
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]91
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]92
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]93
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]7
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]8
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]90
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]91
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]92
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]01
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]0
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]04
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]4
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]8
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]08
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]09
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]11
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]12