Hướng dẫn python permutations length - chiều dài hoán vị python

Nếu bạn cần hoán vị, chắc chắn.

Show
import collections
import itertools

data = """
Max   27 2 Tennis
Tommy 29 1 Football
Tommy 20 1 Rugby
Tommy 8  9 Baseball
"""

# Generate mapping of person -> sports
grouped = collections.defaultdict(set)
for line in data.strip().splitlines():
    line = line.split()
    # line[0]:  name
    # line[-1]: sport
    grouped[line[0]].add(line[-1])

# Walk over the mapping and print out permutations per person.
for person, sports in grouped.items():
    for sport_set in itertools.permutations(sports):
        print(person, sport_set)

Điều này in ra

Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')

itertools cũng có các chức năng cho combinations

Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
0, nếu đó là những gì bạn đang theo đuổi.

Python cung cấp các phương pháp trực tiếp để tìm hoán vị và kết hợp một chuỗi. Các phương pháp này có mặt trong gói itertools.

Permutation 

Đầu tiên nhập gói ITERTOOLS để thực hiện phương thức hoán vị trong Python. Phương thức này lấy một danh sách làm đầu vào và trả về một danh sách đối tượng các bộ dữ liệu có chứa tất cả các hoán vị trong một biểu mẫu danh sách. & Nbsp; & nbsp;
 

Hướng dẫn python permutations length - chiều dài hoán vị python

Python3

Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
1
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
2
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
3
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
4

Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
5
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
6
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
7
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
8
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
9
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
0
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
9
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
223

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
4
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
5
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
6
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
7
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
8

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
9
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
0
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
1

Output:  

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)

Nó tạo ra n! hoán vị nếu độ dài của chuỗi đầu vào là n. & nbsp; nếu muốn & nbsp; để có được hoán vị có độ dài l sau đó thực hiện nó theo cách này. & nbsp; & nbsp;
If want  to get permutations of length L then implement it in this way. 
 

Python3

Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
1
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
2
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
3
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
4

Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
5
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
6
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
7
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
8
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
9
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
0
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
9
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
223

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
4
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
5
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
6
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
7
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
8

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
9
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
0
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
1

Output: 

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)

Nó tạo ra n! hoán vị nếu độ dài của chuỗi đầu vào là n. & nbsp; nếu muốn & nbsp; để có được hoán vị có độ dài l sau đó thực hiện nó theo cách này. & nbsp; & nbsp;

Combination 

Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
5
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
6
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
7
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
8
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
9
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
0
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
9
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
222
 

Python3

Nó tạo ra ncr * r! hoán vị Nếu độ dài của chuỗi đầu vào là n và tham số đầu vào là r.

Phương thức này lấy một danh sách và đầu vào r làm đầu vào và trả về một danh sách đối tượng các bộ dữ liệu chứa tất cả các kết hợp có thể có của độ dài r trong một biểu mẫu danh sách. & Nbsp; & nbsp;

Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
1
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
2
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
3 combinations

(1, 2)
(1, 3)
(2, 3)
9
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
6
(2, 1)
(2, 3)
(1, 3)
1
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
8
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
9
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
0
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
9
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
222

Output:  

(1, 2)
(1, 3)
(2, 3)

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
4
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
5
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
6
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
7
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
4
 

Python3

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
9
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
0
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
7

1. Kết hợp được phát ra theo thứ tự sắp xếp từ vựng của đầu vào. Vì vậy, nếu danh sách đầu vào được sắp xếp, các bộ dữ liệu kết hợp sẽ được tạo theo thứ tự được sắp xếp. & Nbsp; & nbsp;

Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
1
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
2
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
3 itertools1

(1, 2)
(1, 3)
(2, 3)
9
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
6
(2, 1)
(2, 3)
(1, 3)
1
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
8
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
9
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
0
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
9
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
222

Output: 

(1, 2)
(1, 3)
(2, 3)

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
4
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
5
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
6
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
7
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
4
 

Python3

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
9
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
0
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
7

1. Kết hợp được phát ra theo thứ tự sắp xếp từ vựng của đầu vào. Vì vậy, nếu danh sách đầu vào được sắp xếp, các bộ dữ liệu kết hợp sẽ được tạo theo thứ tự được sắp xếp. & Nbsp; & nbsp;

Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
1
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
2
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
3 itertools1

(1, 2)
(1, 3)
(2, 3)
9
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
6
(2, 1)
(2, 3)
(1, 3)
1
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
8
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
9
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
0
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
9
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
222

Output: 

(2, 1)
(2, 3)
(1, 3)

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
4
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
5
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
6
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
7
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
4
 

Python3

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
9
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
0
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
7

1. Kết hợp được phát ra theo thứ tự sắp xếp từ vựng của đầu vào. Vì vậy, nếu danh sách đầu vào được sắp xếp, các bộ dữ liệu kết hợp sẽ được tạo theo thứ tự được sắp xếp. & Nbsp; & nbsp;

Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
1
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
2
Max ('Tennis',)
Tommy ('Baseball', 'Rugby', 'Football')
Tommy ('Baseball', 'Football', 'Rugby')
Tommy ('Rugby', 'Baseball', 'Football')
Tommy ('Rugby', 'Football', 'Baseball')
Tommy ('Football', 'Baseball', 'Rugby')
Tommy ('Football', 'Rugby', 'Baseball')
3 itertools1

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
9
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
0
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
1

Output:

(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)