0

I need to create all combinations of the terms in a list. I've tried using all the parts of itertools, such as permutations, combinations, combinations_with_replacement, but all of them seem to almost 'count' up. For example, using this code:

from itertools import combinations_with_replacement
hex_chars = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
perm = combinations_with_replacement(hex_chars, 5) 
for i in list(perm): 
    print(i)

It produces:

('0', '0', '0', '0', '0')
('0', '0', '0', '0', '1')
('0', '0', '0', '0', '2')
('0', '0', '0', '0', '3')
('0', '0', '0', '0', '4')
('0', '0', '0', '0', '5')
('0', '0', '0', '0', '6')
('0', '0', '0', '0', '7')
('0', '0', '0', '0', '8')
('0', '0', '0', '0', '9')
('0', '0', '0', '0', 'a')
('0', '0', '0', '0', 'b')
('0', '0', '0', '0', 'c')
('0', '0', '0', '0', 'd')
('0', '0', '0', '0', 'e')
('0', '0', '0', '0', 'f')
('0', '0', '0', '1', '1')

I need it to produce literally every possible combination, for example, you'll notice it doesn't produce "000010", and if left for long enough, it won't produce strings such as "A000A". I need to produce all of the combinations (including duplicates) that have a string-length of 5, and then I need to save them all to external text file. And, to clarify, I mean literally every single possible combination, such as "A0000", "0A000", "00A00", "000A00", "0000A0", "00000A".

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • That's not a combination - `00010` is the same as `00001`, as far as combinations is concerned (similarly for `a000a` and `000aa`). – jonrsharpe Oct 15 '19 at 11:15
  • 4
    lst = list(itertools.product(hex_chars, repeat=5)) – ncica Oct 15 '19 at 11:20
  • @jonrsharpe, I need every string combination possible, in every possible sequence, so I need a000a, 0a00a, 00a0a, 000aa, 10000, 01000, 00100, 00010, 00001, and i viewed the duplicate before posting this, and it does not fix my problem –  Oct 15 '19 at 14:25
  • The answer to the duplicate *does* provide that, so please clarify in more detail what you still need to know. – jonrsharpe Oct 15 '19 at 15:06

1 Answers1

-1
from itertools import permutations
perm = permutations(hex_chars, 5)
while a:
    try: 
        print(next(a))
    except:
        pass

Didn't notice the duplicate part at first, I guess you can type each char 5 times

eladgl
  • 69
  • 8