There is a list of numbers s = [1, 2, 3, 4, 5, 6, 7] . It is necessary to compile a two-dimensional array m[7**7][7] , containing in the rows all possible arrangements from s . Implemented a variant with random , but it is very slow ..

 dx = 7 dy = 7 ** 7 m = [[1 for x in range(dx)] for y in range(dy)] i = 0 k = 0 while i < dy: for j in range(dx): m[i][j] = random.randint(1, 7) for d in range(i): if d != i and m[i] == m[d]: i -= 1 k += 1 i += 1 

The same clarification: possible permutations and repetitions of numbers in any place. Those. variant [1, 1, 1, 1, 1, 1, 1] also correct.

    2 answers 2

     import itertools res = list(itertools.product(s, repeat=len(s))) print(len(res)) # 823543 

      Can be done using itertools

       import itertools s = [1, 2, 3] items = list(itertools.permutations(s)) >>> items [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)] 

      As a result, we obtain an array of tuples, each of which contains one of the layout options.

      • Thank. But I completely forgot to clarify an important point: permutations and repetitions of numbers in any place are possible. Those. variant [1, 1, 1, 1, 1, 1, 1] is also correct. - Andrew
      • list(itertools.combinations_with_replacement(s, len(s))) , then you will have 1716 combinations: list(itertools.combinations_with_replacement(s, len(s))) - MaxU
      • @MaxU, for seven numbers there should be 823543 unique combinations. Thank. - Andrew