аватар question@mail.ru · 01.01.1970 03:00

How to get all the combinations of numbers in Python correctly

there is a code:

   IMPORT  ITERTOOLS  for  i  in  itertools.combinations_with_replacement ( '' 0123456789 ',  4 ):   print  ( '' ' .join (i))     

I need to get all the combinations of 4 numbers with repetitions. Part of the output of the program:

   0000  000100020003000500060007000800090011001200 130014001500160017001800190022002300240025    

Tell me, tell me Why after 0009 there is 0011 , and not 0010 ? Also with 0022 .

аватар answer@mail.ru · 01.01.1970 03:00

Because for your task you need to take not combinations_with_replacement , and product :

import itertools for i in itrtools.product Class = ""> '0123456789' , repeat = 4 ): print ( '' .Join (i))

combinatorics do not take into account the order of elements. Therefore, the set of the same elements will be considered the same combination, even if the elements are in a different order. Your code has already printed a combination of three zeros and one unit (0001) once, so it will not print other options for this combination - 0010, 0100 and 1000. Similarly with other numbers.

Latest

Similar