Hướng dẫn iterate over all pairs in list python - lặp qua tất cả các cặp trong danh sách python

Tôi đã có một vấn đề tương tự và tìm thấy giải pháp ở đây. Nó hoạt động mà không phải nhập bất kỳ mô -đun nào.

Giả sử một danh sách như:

people = ["Lisa","Pam","Phil","John"]

Một giải pháp một dòng đơn giản hóa sẽ trông như thế này.

Tất cả các cặp có thể, bao gồm các bản sao:, including duplicates:

result = [foo[p1, p2] for p1 in people for p2 in people]

Tất cả các cặp có thể, không bao gồm các bản sao::

result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]

Các cặp duy nhất, trong đó thứ tự không liên quan:, where order is irrelevant:

result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]

Trong trường hợp bạn không muốn hoạt động nhưng chỉ để có được các cặp, việc loại bỏ chức năng

result = [foo[p1, p2] for p1 in people for p2 in people]
2 và chỉ sử dụng một tuple là đủ.

Tất cả các cặp có thể, bao gồm các bản sao:, including duplicates:

list_of_pairs = [[p1, p2] for p1 in people for p2 in people]

Result:

['Lisa', 'Lisa']
['Lisa', 'Pam']
['Lisa', 'Phil']
['Lisa', 'John']
['Pam', 'Lisa']
['Pam', 'Pam']
['Pam', 'Phil']
['Pam', 'John']
['Phil', 'Lisa']
['Phil', 'Pam']
['Phil', 'Phil']
['Phil', 'John']
['John', 'Lisa']
['John', 'Pam']
['John', 'Phil']
['John', 'John']

Tất cả các cặp có thể, không bao gồm các bản sao::

list_of_pairs = [[p1, p2] for p1 in people for p2 in people if p1 != p2]

Result:

['Lisa', 'Pam']
['Lisa', 'Phil']
['Lisa', 'John']
['Pam', 'Lisa']
['Pam', 'Phil']
['Pam', 'John']
['Phil', 'Lisa']
['Phil', 'Pam']
['Phil', 'John']
['John', 'Lisa']
['John', 'Pam']
['John', 'Phil']

Các cặp duy nhất, trong đó thứ tự không liên quan:, where order is irrelevant:

list_of_pairs = [[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]

Result:

['Lisa', 'Pam']
['Lisa', 'Phil']
['Lisa', 'John']
['Pam', 'Phil']
['Pam', 'John']
['Phil', 'John']

Trong trường hợp bạn không muốn hoạt động nhưng chỉ để có được các cặp, việc loại bỏ chức năng

result = [foo[p1, p2] for p1 in people for p2 in people]
2 và chỉ sử dụng một tuple là đủ.

Đôi khi, trong khi làm việc với danh sách Python, chúng ta có thể gặp vấn đề trong đó chúng ta cần trích xuất tất cả các cặp có thể có thể được thực hiện từ các số nguyên từ danh sách. Loại vấn đề 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]]] : test_list = [1, 7, 4]
Output : [[1, 7], [1, 4], [7, 4]]

Đầu vào: test_list = [7, 4] đầu ra: [[7, 4]]] : test_list = [7, 4]
Output : [[7, 4]]

Phương pháp số 1: Sử dụng danh sách hiểu +

result = [foo[p1, p2] for p1 in people for p2 in people]
3 Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, 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à Enumerate [] được sử dụng để kiểm tra các chỉ số tiếp theo trong khi lặp lại.
This is one of the ways in which this task can be performed. In this, we perform the task of pairing using nested loops in list comprehension recipe, and enumerate[] is used to check with the next indices while iteration.

result = [foo[p1, p2] for p1 in people for p2 in people]
4
result = [foo[p1, p2] for p1 in people for p2 in people]
5
result = [foo[p1, p2] for p1 in people for p2 in people]
6
result = [foo[p1, p2] for p1 in people for p2 in people]
7
result = [foo[p1, p2] for p1 in people for p2 in people]
8
result = [foo[p1, p2] for p1 in people for p2 in people]
9
result = [foo[p1, p2] for p1 in people for p2 in people]
8
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
1
result = [foo[p1, p2] for p1 in people for p2 in people]
8
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
3
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
4

result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
5
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
6
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
7
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
8
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
9
result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
0

result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
1
result = [foo[p1, p2] for p1 in people for p2 in people]
5
result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
3
result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
4
result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
5______36

result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
5
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
6
list_of_pairs = [[p1, p2] for p1 in people for p2 in people]
8
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
8
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
9
['Lisa', 'Lisa']
['Lisa', 'Pam']
['Lisa', 'Phil']
['Lisa', 'John']
['Pam', 'Lisa']
['Pam', 'Pam']
['Pam', 'Phil']
['Pam', 'John']
['Phil', 'Lisa']
['Phil', 'Pam']
['Phil', 'Phil']
['Phil', 'John']
['John', 'Lisa']
['John', 'Pam']
['John', 'Phil']
['John', 'John']
1

Đầu ra:

result = [foo[p1, p2] for p1 in people for p2 in people]
0

Phương pháp số 2: Sử dụng

['Lisa', 'Lisa']
['Lisa', 'Pam']
['Lisa', 'Phil']
['Lisa', 'John']
['Pam', 'Lisa']
['Pam', 'Pam']
['Pam', 'Phil']
['Pam', 'John']
['Phil', 'Lisa']
['Phil', 'Pam']
['Phil', 'Phil']
['Phil', 'John']
['John', 'Lisa']
['John', 'Pam']
['John', 'Phil']
['John', 'John']
2 Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi chỉ sử dụng hàm inbuild để ghép nối và gửi 2 làm giá trị để tạo các cặp có kích thước 2.
This is one of the ways in which this task can be performed. In this, we just using inbuild function for pairing and sending 2 as value for making pairs of size 2.

['Lisa', 'Lisa']
['Lisa', 'Pam']
['Lisa', 'Phil']
['Lisa', 'John']
['Pam', 'Lisa']
['Pam', 'Pam']
['Pam', 'Phil']
['Pam', 'John']
['Phil', 'Lisa']
['Phil', 'Pam']
['Phil', 'Phil']
['Phil', 'John']
['John', 'Lisa']
['John', 'Pam']
['John', 'Phil']
['John', 'John']
3
['Lisa', 'Lisa']
['Lisa', 'Pam']
['Lisa', 'Phil']
['Lisa', 'John']
['Pam', 'Lisa']
['Pam', 'Pam']
['Pam', 'Phil']
['Pam', 'John']
['Phil', 'Lisa']
['Phil', 'Pam']
['Phil', 'Phil']
['Phil', 'John']
['John', 'Lisa']
['John', 'Pam']
['John', 'Phil']
['John', 'John']
4
['Lisa', 'Lisa']
['Lisa', 'Pam']
['Lisa', 'Phil']
['Lisa', 'John']
['Pam', 'Lisa']
['Pam', 'Pam']
['Pam', 'Phil']
['Pam', 'John']
['Phil', 'Lisa']
['Phil', 'Pam']
['Phil', 'Phil']
['Phil', 'John']
['John', 'Lisa']
['John', 'Pam']
['John', 'Phil']
['John', 'John']
5
['Lisa', 'Lisa']
['Lisa', 'Pam']
['Lisa', 'Phil']
['Lisa', 'John']
['Pam', 'Lisa']
['Pam', 'Pam']
['Pam', 'Phil']
['Pam', 'John']
['Phil', 'Lisa']
['Phil', 'Pam']
['Phil', 'Phil']
['Phil', 'John']
['John', 'Lisa']
['John', 'Pam']
['John', 'Phil']
['John', 'John']
6

result = [foo[p1, p2] for p1 in people for p2 in people]
4
result = [foo[p1, p2] for p1 in people for p2 in people]
5
result = [foo[p1, p2] for p1 in people for p2 in people]
6
result = [foo[p1, p2] for p1 in people for p2 in people]
7
result = [foo[p1, p2] for p1 in people for p2 in people]
8
result = [foo[p1, p2] for p1 in people for p2 in people]
9
result = [foo[p1, p2] for p1 in people for p2 in people]
8
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
1
result = [foo[p1, p2] for p1 in people for p2 in people]
8
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
3
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
4

result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
5
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
6
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
7
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
8
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
9
result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
0

result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
1
result = [foo[p1, p2] for p1 in people for p2 in people]
5
result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
3
result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
4
result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
5______36

result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
5
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
6
list_of_pairs = [[p1, p2] for p1 in people for p2 in people]
8
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
8
result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
9
['Lisa', 'Lisa']
['Lisa', 'Pam']
['Lisa', 'Phil']
['Lisa', 'John']
['Pam', 'Lisa']
['Pam', 'Pam']
['Pam', 'Phil']
['Pam', 'John']
['Phil', 'Lisa']
['Phil', 'Pam']
['Phil', 'Phil']
['Phil', 'John']
['John', 'Lisa']
['John', 'Pam']
['John', 'Phil']
['John', 'John']
1

Đầu ra:

result = [foo[p1, p2] for p1 in people for p2 in people]
0


Làm thế nào để bạn tìm thấy tất cả các cặp trong Python?

Đầu tiên, chúng tôi lấy một danh sách trống 'res' và bắt đầu một vòng lặp và đi qua từng yếu tố của danh sách số nguyên đã cho. Trong mỗi lần lặp, bật phần tử, lưu trữ nó trong 'num', tìm sự khác biệt còn lại cho tổng K và kiểm tra xem sự khác biệt có tồn tại trong danh sách đã cho hay không.

Làm thế nào để bạn lặp qua một cặp trong Python?

Giải pháp: Lặp lại trong danh sách Python LST theo cặp, lặp lại tất cả các chỉ số danh sách I từ 0 đến chỉ mục của phần tử danh sách cuối cùng thứ hai [bao gồm]. Trong thân vòng, sử dụng các chỉ số để truy xuất các cửa sổ bằng cách cắt LST [I: I+2]. Cắt có hiệu quả về bộ nhớ vì nó không tạo ra các bản sao của danh sách ban đầu.iterate over all list indices i from 0 to the index of the second last list element [included]. In the loop body, use the indices to retrieve the windows by slicing lst[i:i+2] . Slicing is memory-efficient because it doesn't create copies of the original list.

Làm thế nào để bạn tìm thấy một cặp trong một danh sách?

Để tìm tất cả các cặp có thể từ mảng, chúng ta cần đi qua mảng và chọn phần tử đầu tiên của cặp. Sau đó chúng ta cần ghép phần tử này với tất cả các phần tử trong mảng từ chỉ mục 0 đến n-1.Dưới đây là cách tiếp cận từng bước: Đi qua mảng và chọn một phần tử trong mỗi lần đi ngang.traverse the array and select the first element of the pair. Then we need to pair this element with all the elements in the array from index 0 to N-1. Below is the step by step approach: Traverse the array and select an element in each traversal.

Làm thế nào để bạn nối vào một danh sách trong Python?

Danh sách Python cộng đồng []..
Cú pháp của danh sách append [] Cú pháp của phương thức append [] là: list.append [mục].
tham số chắp thêm [].Phương thức có một đối số duy nhất ..
Trả về giá trị từ append [] Phương thức không trả về bất kỳ giá trị nào [trả về không] ..
Ví dụ 1: Thêm phần tử vào danh sách.# Động vật liệt kê động vật = ['Cat', 'Dog', 'Rabbit'].

Bài Viết Liên Quan

Chủ Đề