Python thêm hai bộ dữ liệu lại với nhau

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

Bạn có thể thêm 2 bộ dữ liệu lại với nhau không?

Thêm bộ . Hoạt động bổ sung chỉ đơn giản là thực hiện nối với các bộ dữ liệu. Bạn chỉ có thể thêm hoặc kết hợp các loại dữ liệu giống nhau. You can combine tuples to form a new tuple. The addition operation simply performs a concatenation with tuples. You can only add or combine same data types.

Bạn có thể sử dụng += cho bộ dữ liệu không?

Tại sao danh sách Python cho phép bạn += một bộ, trong khi bạn không thể + một bộ? . Không. Cố gắng thêm danh sách và bộ dữ liệu, ngay cả khi chúng tôi không ảnh hưởng đến cả hai, dẫn đến lỗi trên. Đúng rồi. Thêm danh sách vào bộ với + không hoạt động .

Bạn có thể nối các bộ dữ liệu trong Python không?

Bộ dữ liệu là bất biến, mặc dù bạn có thể sử dụng toán tử + để nối nhiều bộ dữ liệu

Chủ Đề