2

I am trying to work out how to pass itertools.product a dynamic number of arguments.

I have the following code which worked as expected printing out lines with each line having 4 characters in a different order:

#!/usr/bin/env python3.5
import sys, itertools, multiprocessing, functools

alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12234567890!@#$%^&*?,()-=+[]/;"
num_parts = 4
part_size = len(alphabet) // num_parts

def do_job(first_bits):
    for x in itertools.product(first_bits, alphabet, alphabet, alphabet):
        print(''.join(x))

if __name__ == '__main__':
    pool = multiprocessing.Pool(processes=4)
    results = []

    for i in range(num_parts):
        if i == num_parts - 1:
            first_bit = alphabet[part_size * i :]
        else:
            first_bit = alphabet[part_size * i : part_size * (i+1)]
        pool.apply_async(do_job, (first_bit,))

    pool.close()
    pool.join()

Then I tried to make it completely dynamic using the following code, where the number of alphabet arguments is created on the fly based on the num_parts variable:

#!/usr/bin/env python3.5
import sys, itertools, multiprocessing, functools

alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12234567890!@#$%^&*?,()-=+[]/;"
num_parts = 4
part_size = len(alphabet) // num_parts
dynamicArgs = []

def do_job(first_bits):
    for x in itertools.product(first_bits, *dynamicArgs):
        print(''.join(x))

if __name__ == '__main__':
    pool = multiprocessing.Pool(processes=4)
    results = []
    for x in range(num_parts-1):
        dynamicArgs.append(alphabet)

    for i in range(num_parts):
        if i == num_parts - 1:
            first_bit = alphabet[part_size * i :]
        else:
            first_bit = alphabet[part_size * i : part_size * (i+1)]
        pool.apply_async(do_job, (first_bit,))

    pool.close()
    pool.join()

But this does not work as expected... it outputs lines with one character on each line and iterates over the alphabet only a single time.

How can I pass a dynamic number of alphabet variables as arguments to itertools.product ?

Thanks for your time.

5
  • 1
    I'm confused. What is the expected output? And what do you mean by dynamic? Commented Jun 7, 2016 at 23:49
  • The expected output is that the third attempt would have the same output as the first two attempts... eg; it would print lines in the console having 4 characters on each line. Commented Jun 7, 2016 at 23:57
  • Where is the code that is calling do_job()? Commented Jun 7, 2016 at 23:59
  • I will update the example with the full code give me a min Commented Jun 8, 2016 at 0:00
  • For me, the third attempt does have the same output as the first two attempts. Commented Jun 8, 2016 at 0:02

2 Answers 2

1

You can just multiply the list of strings:

 def do_job(first_bits):

    for x in itertools.product(first_bits, *[alphabet] * 3):

You could also use itertools.repeat:

from itertools import repeat
def do_job(first_bits, times):
    for x in itertools.product(first_bits, *repeat(alphabet, times)):
Sign up to request clarification or add additional context in comments.

Comments

0

Is this what you are trying to do?

import sys, itertools, multiprocessing, functools                               

alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12234567890!@#$%^&*?,()-=+[]/;"

repeats = 5                                                                     

alphabets = [ alphabet ] * repeats                                              

def do_job(first_bits):                                                         

    for x in itertools.product(first_bits, *alphabets):                         
        print(''.join(x))                                                       

do_job('12345')         

You can use the * operator on arrays which repeats the elements.

Output:

1aaaaa
1aaaab
1aaaac
1aaaad
1aaaae
1aaaaf
1aaaag
1aaaah
1aaaai
1aaaaj
1aaaak
[...]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.