Tập hợp các cặp trong Python

Đôi khi, trong khi làm việc với danh sách Python, chúng ta có thể gặp sự cố trong đó chúng ta cần trích xuất tất cả các cặp có thể được thực hiện từ các số nguyên từ danh sách. Loại sự cố này có thể xảy ra trong nhiều lĩnh vực như lập trình hàng ngày và phát triển web. 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

Đầu vào. test_list = [1, 7, 4]
Đầu ra. [[1, 7], [1, 4], [7, 4]]

Đầu vào. test_list = [7, 4]
Đầu ra. [[7, 4]]

Phương pháp #1. Sử dụng khả năng hiểu danh sách +

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
28
Đây là một trong những cách mà tác vụ này có thể được thực hiện. Trong phần này, chúng tôi thực hiện nhiệm vụ ghép nối bằng cách sử dụng các vòng lặp lồng nhau trong công thức hiểu danh sách và liệt kê [] được sử dụng để kiểm tra các chỉ số tiếp theo trong khi lặp lại.

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
29
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
30
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
31
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
32
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
1
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
3
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
5
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
6

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
7
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
8
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
9
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
1
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
2

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
3
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
30
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
5
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
6
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
7
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
8
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
9
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
90
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
6
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
92
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
8
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
94
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
32
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
97

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
7__
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
8
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
90
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
1
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
93

đầu ra

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]

Phương pháp #2. Sử dụng

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
94
Đây là một trong những cách thực hiện nhiệm vụ này. Trong trường hợp này, chúng tôi chỉ sử dụng chức năng sẵn có để ghép nối và gửi 2 làm giá trị để tạo các cặp có kích thước 2.

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
95
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
96
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
97
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
98

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
29
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
30
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
31
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
32
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
1
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
3
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
5
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
6

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
7
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
8
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
9
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
1
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
2

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
3
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
30
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
298
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
299
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
300
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
301

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
7__
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
8
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
90
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
1
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
93

đầu ra

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]


Lặp lại danh sách là phổ biến trong lập trình Python, nhưng đôi khi người ta yêu cầu in các phần tử theo cặp liên tiếp. Vấn đề cụ thể này khá phổ biến và có một giải pháp cho nó luôn hữu ích. Hãy thảo luận về một số cách mà vấn đề này có thể được giải quyết.  

Lặp lại cặp trong một danh sách bằng cách sử dụng khả năng hiểu danh sách

Khả năng hiểu danh sách có thể được sử dụng để in các cặp bằng cách truy cập các phần tử hiện tại và phần tử tiếp theo trong danh sách, sau đó in phần tử tương tự. Phải cẩn thận khi ghép nối phần tử cuối cùng với phần tử đầu tiên để tạo thành một cặp tuần hoàn.  

Python3

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
95
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
96
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
97
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
311

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
29
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
30
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
31
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
315
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
32
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
300
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
5
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
3
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
325
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
6

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
7
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
8
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
329
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
1
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
2

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
3
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
30
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
05
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
32
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
08
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
09
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
10
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
2

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
12
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
6
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
14
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
8
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
16
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
8
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
10
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
19

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
7
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
8
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
22
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
1
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
93

đầu ra

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
9

Lặp lại cặp trong danh sách bằng cách sử dụng zip[] + cắt danh sách

Chức năng zip có thể được sử dụng để trích xuất các cặp trong danh sách cắt có thể được sử dụng để ghép nối liên tiếp phần tử hiện tại với phần tử tiếp theo để ghép nối hiệu quả.  

Python3

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
95
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
96
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
97
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
311

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
29
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
30
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
31
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
315
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
32
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
300
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
5
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
3
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
325
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
6

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
7
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
8
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
329
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
1
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
2

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
3
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
30
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
298
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
8
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
55
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
56
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
32
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
58
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
60
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
32
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
62

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
7
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
8
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
22
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
0
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
1
The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
93

đầu ra

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]
9

Làm cách nào để tạo một danh sách cặp trong Python?

Trong bài viết này, chúng ta sẽ tìm hiểu cách tạo các cặp từ hai danh sách sao cho không có phần tử giống nhau nào tạo thành một cặp. .

Khởi tạo danh sách với các phần tử

Lặp lại các danh sách và nối cặp vào danh sách nếu các phần tử tương ứng từ các danh sách không giống nhau

Các cặp Python là gì?

Các hàm ghép nối lấy hai số nguyên và trả lại cho bạn một số nguyên . Điều làm cho chức năng ghép nối trở nên đặc biệt là nó có thể đảo ngược; .

Có một cặp trong Python?

Ghép nối lặp trong danh sách bằng cách sử dụng zip[] + cắt danh sách . Chức năng zip có thể được sử dụng để trích xuất các cặp trong danh sách cắt có thể được sử dụng để ghép nối liên tiếp phần tử hiện tại với phần tử tiếp theo để ghép nối hiệu quả.

Việc sử dụng set[] trong Python là gì?

set[] được sử dụng để chuyển đổi bất kỳ phần tử có thể lặp lại nào thành chuỗi các phần tử có thể lặp lại với các phần tử riêng biệt , thường được gọi là Set. Thông số. Bất kỳ chuỗi có thể lặp lại nào như danh sách, bộ dữ liệu hoặc từ điển. trả lại. Tập rỗng nếu không có phần tử nào được truyền.

Cặp được sắp xếp trong Python là gì?

Một cặp có thứ tự là tổ hợp gồm tọa độ x [abscissa] và tọa độ y [tọa độ], có hai giá trị được viết theo thứ tự cố định trong ngoặc đơn.

Chủ Đề