Home > database >  how to use itertools methods so that they show all possible combinations
how to use itertools methods so that they show all possible combinations

Time:09-21

I need to find absolutely all possible combinations, including taking into account their position. for example, the combination 222, 200, 220 does not come out in any way. what else can be done?

Here is mu code:

import itertools

real_code = '220'
code = itertools.permutations('012', r=3)
set_of_code = set()
for i in code:
    set_of_code.add(''.join(i))
    # print('permut', i)

code2 = itertools.combinations_with_replacement('012', r=3)
for i in code2:
    set_of_code.add(''.join(i))
    # print('comboW', i)

for i in set_of_code:
    if i == real_code:
        print('found', i)
        break
else:
    print('not found')

CodePudding user response:

This should give you the desired combinations:

code = itertools.product('012', repeat=3)
  • Related