1# 1. Print all combinations
2from itertools import combinations
3
4comb = combinations([1, 1, 3], 2)
5print(list(combinations([1, 2, 3], 2)))
6# Output: [(1, 2), (1, 3), (2, 3)]
7
8# 2. Counting combinations
9from math import comb
10print(comb(10,3))
11#Output: 120
12
1all_combinations = [list(zip(each_permutation, list2)) for each_permutation in itertools.permutations(list1, len(list2))]
1# A Python program to print all
2# permutations using library function
3from itertools import permutations
4
5# Get all permutations of [1, 2, 3]
6perm = permutations([1, 2, 3])
7
8# Print the obtained permutations
9for i in list(perm):
10 print (i)
11
1import itertools
2def subs(l):
3 res = []
4 for i in range(1, len(l) + 1):
5 for combo in itertools.combinations(l, i):
6 res.append(list(combo))
7 return res
8
1def permutations(iterable, r=None):
2 # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
3 # permutations(range(3)) --> 012 021 102 120 201 210
4 pool = tuple(iterable)
5 n = len(pool)
6 r = n if r is None else r
7 if r > n:
8 return
9 indices = list(range(n))
10 cycles = range(n, n-r, -1)
11 yield tuple(pool[i] for i in indices[:r])
12 while n:
13 for i in reversed(range(r)):
14 cycles[i] -= 1
15 if cycles[i] == 0:
16 indices[i:] = indices[i+1:] + indices[i:i+1]
17 cycles[i] = n - i
18 else:
19 j = cycles[i]
20 indices[i], indices[-j] = indices[-j], indices[i]
21 yield tuple(pool[i] for i in indices[:r])
22 break
23 else:
24 return
25