Python lấy tất cả các cặp duy nhất từ ​​​​danh sách

Chủ đề này là về việc tìm các kết hợp duy nhất trong khi chủ đề kia là tìm TẤT CẢ các kết hợp

Nếu tôi có một danh sách trăn

 L = [1,2,3,4]

cách tốt nhất để có được tất cả các kết hợp độc đáo có thể có của 3 yếu tố từ danh sách như bên dưới

["1,2,3", "1,2,4", "2,3,4", "3,4,1"]

Thứ tự của các yếu tố trong các kết hợp không quan trọng. Ví dụ:

["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
07 và
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
08 sẽ được coi là cùng một tổ hợp

Tôi có thể viết một vài vòng lặp để làm điều này nhưng tôi nghĩ có thể có một lớp lót có thể làm điều tương tự

Tôi đã cố lấy tất cả các kết hợp cặp duy nhất từ ​​một danh sách

Đây là những gì tôi đã làm cho đến nay,

import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]

trường hợp thử nghiệm

sample = ["apple", "orange", "banana", "grapes", "mango"]
uniqueCombinations[sample]

đầu ra

[['apple', 'banana'],
 ['orange', 'mango'],
 ['orange', 'grapes'],
 ['apple', 'grapes'],
 ['orange', 'banana'],
 ['apple', 'mango'],
 ['grapes', 'mango'],
 ['apple', 'orange'],
 ['banana', 'mango'],
 ['banana', 'grapes']]

Đầu vào sẽ là các chuỗi danh sách duy nhất. Giả sử rằng các yếu tố trùng lặp sẽ không được thông qua

Có cách nào tốt hơn/thanh lịch hơn/chính xác hơn để làm điều này không??

Tạo danh sách các cặp duy nhất [a, b] từ N phần tử đảm bảo tất cả các phần tử tham gia vào a và b

Tệp này chứa văn bản Unicode hai chiều có thể được diễn giải hoặc biên dịch khác với nội dung hiển thị bên dưới. Để xem lại, hãy mở tệp trong trình chỉnh sửa hiển thị các ký tự Unicode bị ẩn. Tìm hiểu thêm về các ký tự Unicode hai chiều

nhập sys , ngẫu nhiênfout = mở [ 'cặp. txt' , 'w' ] # tổng số người tham giatotal_people = int [ sys . argv [ 1 ]] a , b = [], [] cho i in xrange [ 1 , total_people+1 ]. a . append [ i ] trong khi Đúng . r = ngẫu nhiên . randint [ 1 , total_people ] nếu r không in b r . = i . b . chắp thêm [ r ] phá vỡcho i , j in zip [ a , b ]. fout . viết [ str [ i ] +" "+str [ j ] +" \n " ] fout . đóng[]

Đưa ra một danh sách. Nhiệm vụ là viết chương trình Python để lấy tất cả các kết hợp theo cặp từ danh sách

Tìm tất cả các cặp [Không có tính duy nhất]

Thí dụ

Đầu vào. [1,"Mallika",2,"Yash"]

đầu ra. [[1, 'Mallika'], [1, 2], [1, 'Yash'], ['Mallika', 1], ['Mallika', 2], ['Mallika', 'Yash'], [

Phương pháp 1. Sử dụng các vòng lặp đơn giản

Chúng tôi có thể truy cập tất cả các kết hợp của danh sách bằng cách sử dụng hai vòng lặp để lặp qua các chỉ mục danh sách. Nếu cả hai bộ đếm chỉ mục có cùng giá trị chỉ mục, chúng tôi bỏ qua nó, nếu không, chúng tôi in phần tử tại chỉ mục i theo sau là phần tử tại chỉ mục j theo thứ tự.  

Độ phức tạp về thời gian của phương pháp này là O[n2] vì chúng tôi yêu cầu hai vòng lặp để lặp qua danh sách.  

Python3

["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
09
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
0
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
1
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
2
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
3
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
4
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
3
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
6
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
3
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
8
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
9

import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
0
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
0
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
2

import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
3
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
4
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
5
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
6
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
7
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
8
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
3
sample = ["apple", "orange", "banana", "grapes", "mango"]
uniqueCombinations[sample]
0
sample = ["apple", "orange", "banana", "grapes", "mango"]
uniqueCombinations[sample]
1

sample = ["apple", "orange", "banana", "grapes", "mango"]
uniqueCombinations[sample]
2
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
3
sample = ["apple", "orange", "banana", "grapes", "mango"]
uniqueCombinations[sample]
4
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
5
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
6
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
7
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
8
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
3
sample = ["apple", "orange", "banana", "grapes", "mango"]
uniqueCombinations[sample]
0
sample = ["apple", "orange", "banana", "grapes", "mango"]
uniqueCombinations[sample]
1

[['apple', 'banana'],
 ['orange', 'mango'],
 ['orange', 'grapes'],
 ['apple', 'grapes'],
 ['orange', 'banana'],
 ['apple', 'mango'],
 ['grapes', 'mango'],
 ['apple', 'orange'],
 ['banana', 'mango'],
 ['banana', 'grapes']]
2
[['apple', 'banana'],
 ['orange', 'mango'],
 ['orange', 'grapes'],
 ['apple', 'grapes'],
 ['orange', 'banana'],
 ['apple', 'mango'],
 ['grapes', 'mango'],
 ['apple', 'orange'],
 ['banana', 'mango'],
 ['banana', 'grapes']]
3
[['apple', 'banana'],
 ['orange', 'mango'],
 ['orange', 'grapes'],
 ['apple', 'grapes'],
 ['orange', 'banana'],
 ['apple', 'mango'],
 ['grapes', 'mango'],
 ['apple', 'orange'],
 ['banana', 'mango'],
 ['banana', 'grapes']]
4____10
[['apple', 'banana'],
 ['orange', 'mango'],
 ['orange', 'grapes'],
 ['apple', 'grapes'],
 ['orange', 'banana'],
 ['apple', 'mango'],
 ['grapes', 'mango'],
 ['apple', 'orange'],
 ['banana', 'mango'],
 ['banana', 'grapes']]
6

[['apple', 'banana'],
 ['orange', 'mango'],
 ['orange', 'grapes'],
 ['apple', 'grapes'],
 ['orange', 'banana'],
 ['apple', 'mango'],
 ['grapes', 'mango'],
 ['apple', 'orange'],
 ['banana', 'mango'],
 ['banana', 'grapes']]
7
[['apple', 'banana'],
 ['orange', 'mango'],
 ['orange', 'grapes'],
 ['apple', 'grapes'],
 ['orange', 'banana'],
 ['apple', 'mango'],
 ['grapes', 'mango'],
 ['apple', 'orange'],
 ['banana', 'mango'],
 ['banana', 'grapes']]
8

[['apple', 'banana'],
 ['orange', 'mango'],
 ['orange', 'grapes'],
 ['apple', 'grapes'],
 ['orange', 'banana'],
 ['apple', 'mango'],
 ['grapes', 'mango'],
 ['apple', 'orange'],
 ['banana', 'mango'],
 ['banana', 'grapes']]
9
[['apple', 'banana'],
 ['orange', 'mango'],
 ['orange', 'grapes'],
 ['apple', 'grapes'],
 ['orange', 'banana'],
 ['apple', 'mango'],
 ['grapes', 'mango'],
 ['apple', 'orange'],
 ['banana', 'mango'],
 ['banana', 'grapes']]
50

đầu ra

[[1, 'Mallika'], [1, 2], [1, 'Yash'], ['Mallika', 1], ['Mallika', 2], ['Mallika', 'Yash'], [

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

Python cung cấp hỗ trợ thư viện chuẩn itertools được sử dụng để tạo các trình vòng lặp để lặp hiệu quả. Thư viện cung cấp hỗ trợ cho các loại lặp lại khác nhau, theo nhóm, thứ tự được sắp xếp, v.v. Các hàm hoán vị [] của thư viện này được sử dụng để duyệt qua tất cả các thứ tự có thể có của danh sách các phần tử mà không có bất kỳ sự lặp lại nào. Các hàm permutations[] có cú pháp như sau

[['apple', 'banana'],
 ['orange', 'mango'],
 ['orange', 'grapes'],
 ['apple', 'grapes'],
 ['orange', 'banana'],
 ['apple', 'mango'],
 ['grapes', 'mango'],
 ['apple', 'orange'],
 ['banana', 'mango'],
 ['banana', 'grapes']]
5

Trong đó r mô tả các bộ có độ dài r, tức là, 2 mô tả một cặp, 3 mô tả một bộ ba. Đối số đầu tiên là danh sách được chỉ định.  

Hàm trả về danh sách các nhóm phần tử được trả về sau khi tạo hoán vị. Đầu ra chứa n x [n-1] số phần tử, trong đó n là kích thước của danh sách vì mỗi phần tử sau đó được nhân với tất cả các phần tử khác. Thời gian cần thiết để tính toán các hoán vị gần như theo cấp số nhân theo thứ tự kích thước của danh sách.  

Python3

________ 451 ________ 452

["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
09
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
0
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
1
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
2
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
3
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
4
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
3
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
6
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
3
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
8
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
9

["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
94
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
0
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
96______16
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
98

[['apple', 'banana'],
 ['orange', 'mango'],
 ['orange', 'grapes'],
 ['apple', 'grapes'],
 ['orange', 'banana'],
 ['apple', 'mango'],
 ['grapes', 'mango'],
 ['apple', 'orange'],
 ['banana', 'mango'],
 ['banana', 'grapes']]
9
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
7____1071
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
072

đầu ra

[[1, 'Mallika'], [1, 2], [1, 'Yash'], ['Mallika', 1], ['Mallika', 2], ['Mallika', 'Yash'], [

Ghi chú.  

  • Các cặp được in theo thứ tự xuất hiện của các phần tử trong danh sách
  • Trong trường hợp tất cả các phần tử giống nhau, phương thức vẫn tiếp tục tạo thành các cặp và trả về chúng, ngay cả khi chúng trùng lặp

Python3

________ 451 ________ 452

["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
09
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
0
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
1
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
6
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
3
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
6
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
3
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
6
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
9

["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
084
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
0
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
96____16
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
98

import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
3
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
4
import itertools

# Unique Combination Pairs for list of elements
def uniqueCombinations[list_elements]:
    l = list[itertools.combinations[list_elements, 2]]
    s = set[l]
    # print['actual', len[l], l]
    return list[s]
5
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
092

sample = ["apple", "orange", "banana", "grapes", "mango"]
uniqueCombinations[sample]
2____49
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
095

đầu ra

["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
9

Tìm tất cả các cặp duy nhất [Uniqueness]

Tuy nhiên, phương thức hoán vị không phân biệt giữa các cặp [a, b] và [b, a] và trả về cả hai. Thư viện itertools cũng hỗ trợ phương thức kết hợp [] in một trong hai cặp [a, b] hoặc [b, a] chứ không phải cả hai. Số phần tử xuất ra tương đương với [n-1]. trong đó n là độ dài của danh sách. Thời gian cần thiết để tính toán các kết hợp là gần như đa thức.  

Thí dụ

Đầu vào. [1,"Mallika",2,"Yash"]

đầu ra. [[1, 'Mallika'], [1, 2], [1, 'Yash'], ['Mallika', 2], ['Mallika', 'Yash'], [2, 'Yash']]

Python3

________ 451 ________ 452

________ 1098 ________ 1099 ________ 100 ________ 101

["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
09
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
0
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
1
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
2
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
3
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
4
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
3
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
6
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
3
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
8
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
9

["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
94
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
0
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
15____16
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
98

________ 49 ________ 27 ________ 1071 ________ 1072

đầu ra

[[1, 'Mallika'], [1, 2], [1, 'Yash'], ['Mallika', 2], ['Mallika', 'Yash'], [2, 'Yash']]


Làm cách nào để bạn tạo một cặp từ danh sách 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

Chủ Đề