How to generate a permutation of list of lists in python -
i have list of lists say
[[2, 4, 6], [2, 6, 10], [2, 12, 22], [4, 6, 8], [4, 8, 12], [6, 8, 10], [8, 10, 12], [8, 15, 22], [10, 11, 12]] how generate combination of lists given length?
for example if given length 3
then need combinations of 3 list element scenarios given list of list.
example :
[2, 4, 6], [2, 6, 10], [2, 12, 22] or
[2, 4, 6], [8, 10, 12], [10, 11, 12] ... ... , forth and if given length 2 should like
[2, 4, 6], [2, 6, 10] or
[6, 8, 10], [8, 10, 12] ... ... , forth i dont want permutation of elements inside lists want permutation of lists itself.
there 2 options here. first use itertools.permutations. give every permutation (i.e: [1,2] , [2,1] would not same)
import itertools lists = [[2, 4, 6], [2, 6, 10], [2, 12, 22], [4, 6, 8], [4, 8, 12], [6, 8, 10], [8, 10, 12], [8, 15, 22], [10, 11, 12]] n = 3 perm in itertools.permutations(lists, n): print(perm) if want unique groupings, no duplicates , use itertools.combinations (i.e: [1,2] , [2,1] would same).
for comb in itertools.combinations(lists, n): print(comb)
Comments
Post a Comment