1
votes

I'm trying to find a way to write a Python script wherein I can produce a file containing a list of entries (strings) which are permutations of specific inputs. To elaborate:

Imagine you have 9 digits in a string. But the only possible value in each digit is within the range of 0123456789. So I want all permutations of 0123456789 over 9 digits...and a string list of all those permutations (i.e. 1,000,000,000 possible permutations).

Simple enough to use itertools.permutations right? No. According to the official documentation:

def permutations(iterable, r=None):
    # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC

If you look closely at the results there are no repeats...so no instances of say AA or BB. That means if I try to run all permutations of 0123456789 over 9 digits I won't get the combinations "11111111" or "111222333". I.e. I won't get my 1 billion results, right?

Am I misunderstanding the itertools module here? What can I do to create a file with a string list of all possible permutations of '0123456789' over 9 digits?

2
I think you're looking for itertools.product - zehnpaard
Can't you just use an integer counter and pad to 9 digits? e.g. '{:09}'.format(counter)' where counter` is some number between 0 and 999999999. (What you're looking for is the Cartesian product, but that seems overkill here.) - Rufflewind
Don't forget, that if you want to have this list of strings in memory it will occupy about 40 GiB. (or 10-20 GiB on disk) - Kolmar

2 Answers

4
votes

In normal mathematical terms, 'AA' is not a permutation of 'ABCD'. You may be looking for a Cartesian Product instead -- and itertools supports that, too. E.g:

>>> for x in it.product('ABC', repeat=len('ABC')): print(x)
('A', 'A', 'A')
('A', 'A', 'B')
('A', 'A', 'C')
('A', 'B', 'A')
('A', 'B', 'B')
('A', 'B', 'C')
('A', 'C', 'A')
('A', 'C', 'B')
('A', 'C', 'C')
('B', 'A', 'A')
('B', 'A', 'B')
('B', 'A', 'C')
('B', 'B', 'A')
('B', 'B', 'B')
('B', 'B', 'C')
('B', 'C', 'A')
('B', 'C', 'B')
('B', 'C', 'C')
('C', 'A', 'A')
('C', 'A', 'B')
('C', 'A', 'C')
('C', 'B', 'A')
('C', 'B', 'B')
('C', 'B', 'C')
('C', 'C', 'A')
('C', 'C', 'B')
('C', 'C', 'C')

Note that these definitely aren't permutations -- the number of permutations of 3 items is 3 factorial, i.e 6, while the number of these items in the cartesian product of 3 copies of the 3-items sequence is 3 to the third power, AKA 81. Quite a difference!-)

0
votes

For anyone else looking for something similar, this is the code I wrote. Works fine.

import itertools

print ('')
characters = input('Characters to include: ')
length = input('Length of each combination: ')
filename = input('Name of list file: ')
print ('')

length = int(length)

f1 = open(filename, 'w')
for p in itertools.product(characters,repeat=length):
    f1.write(''.join(str(x) for x in p) + '\n')
f1.close()