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 +
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.result = [foo[p1, p2] for p1 in people for p2 in people]
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]
4result = [foo[p1, p2] for p1 in people for p2 in people]
5 result = [foo[p1, p2] for p1 in people for p2 in people]
6result = [foo[p1, p2] for p1 in people for p2 in people]
7result = [foo[p1, p2] for p1 in people for p2 in people]
8result = [foo[p1, p2] for p1 in people for p2 in people]
9result = [foo[p1, p2] for p1 in people for p2 in people]
8result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
1result = [foo[p1, p2] for p1 in people for p2 in people]
8result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
3result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
4result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
5result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
6result = [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]
9result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
0result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
1result = [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]]]
3result = [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______36result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
5result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
6list_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]
0Phương pháp số 2: Sử dụng
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.['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']
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']
6result = [foo[p1, p2] for p1 in people for p2 in people]
4result = [foo[p1, p2] for p1 in people for p2 in people]
5 result = [foo[p1, p2] for p1 in people for p2 in people]
6result = [foo[p1, p2] for p1 in people for p2 in people]
7result = [foo[p1, p2] for p1 in people for p2 in people]
8result = [foo[p1, p2] for p1 in people for p2 in people]
9result = [foo[p1, p2] for p1 in people for p2 in people]
8result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
1result = [foo[p1, p2] for p1 in people for p2 in people]
8result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
3result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
4result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
5result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
6result = [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]
9result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
0result = [foo[people[p1], people[p2]] for p1 in range[len[people]] for p2 in range[p1+1,len[people]]]
1result = [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]]]
3result = [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______36result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
5result = [foo[p1, p2] for p1 in people for p2 in people if p1 != p2]
6list_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