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

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 lặp lại thông qua một cặp trong danh sách Python?

Sử dụng zip [] để lặp qua một cặp danh sách khi mã xác định hai biến trong vòng lặp của nó kết hợp với một cuộc gọi đến zip [số, chữ cái] trên cặp danh sách, Python tự động gán biến đầu tiên làm giá trị tiếp theo trong Danh sách đầu tiên và biến thứ hai là giá trị tiếp theo trong danh sách thứ hai. When the code defines two variables in its for loop in conjunction with a call to zip[numbers, letters] on the pair of lists, Python automatically assigns the first variable as the next value in the first list, and the second variable as the next value in the second list.

Làm thế nào để tôi lặp lại cứ sau 2 trong Python?

Sử dụng lập chỉ mục danh sách để lặp lại mỗi hai yếu tố trong một danh sách.Sử dụng danh sách danh sách danh sách [0 :: 2] Để trả về mọi yếu tố thứ hai của danh sách, bắt đầu tại INDEX 0. to iterate every two elements in a list. Use list indexing list[0::2] to return every second element of list , starting at index 0 .

Làm thế nào để bạn chọn mọi mục khác trong danh sách Python?

Sử dụng Enumerate [] để truy cập vào mọi yếu tố khác trong danh sách {Sử dụng-for-loop-erumerate} Sử dụng cú pháp cho chỉ mục, phần tử trong liệt kê [có thể lặp lại] để lặp lại thông qua danh sách có thể truy cập được và truy cập từng chỉ mục với phần tử tương ứng của nó.Ở mỗi lần lặp, sử dụng cú pháp nếu index % 2 == 0 để kiểm tra xem chỉ mục có chẵn không. {use-for-loop-enumerate} Use the syntax for index, element in enumerate[iterable] to iterate through the list iterable and access each index with its corresponding element . At each iteration, use the syntax if index % 2 == 0 to check if index is even.

Làm thế nào để bạn xóa một mục khỏi danh sách trong Python?

Phương thức Remove [] xóa phần tử khớp đầu tiên [được truyền dưới dạng đối số] khỏi danh sách.Phương thức pop [] loại bỏ một phần tử tại một chỉ mục nhất định và cũng sẽ trả về mục đã xóa.Bạn cũng có thể sử dụng từ khóa DEL trong Python để xóa một phần tử hoặc lát khỏi danh sách.. The pop[] method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.

Bài Viết Liên Quan

Chủ Đề