dorukayhan
0
Q:

how to add combination in python through functions

# A Python program to print all combinations  
# of given length with duplicates in input 
from itertools import combinations 
  
# Get all combinations of [1, 1, 3] 
# and length 2 
comb = combinations([1, 1, 3], 2) 
  
# Print the obtained combinations 
for i in list(comb): 
    print i 
2
>>> from itertools import permutations
>>> perm = permutations([1, 2, 3])
>>> for i in list(perm): 
...     print(i)
... 
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
0
all_combinations = [list(zip(each_permutation, list2)) for each_permutation in itertools.permutations(list1, len(list2))]
1

New to Communities?

Join the community