0

I'm following the answer by F Sebastian at; Python multiprocessing pool.map for multiple arguments which is using multiprocessing to compute on a function. I'm building up in the argument input, and I'd like my fixed input to the function_star to be '1', '2' and the varying input the 'x' and 'y', howeever when I try the following:

args = itertools.izip(('x', 'y'), itertools.repeat(('1', '2')))

I get the following:

('x', ('1', '2')) ('y', ('1', '2'))

But I'd like to join up the tuples within the itertools, so that I can send it off to:

pool.map(func_star, args)

I'd like something that is more akin to as the first element from the itertools:

('x', '1', '2')...

so my function_star has convert this one argument to three.

Community
  • 1
  • 1
disruptive
  • 5,687
  • 15
  • 71
  • 135
  • I think you're trying to flatten up the tuples. You can look at this [previous](http://stackoverflow.com/questions/18500541/how-to-flatten-a-tuple-in-python) question to get some ideas. Let me know if that works for you. – Sidharth Shah Oct 01 '14 at 11:41

3 Answers3

1

I think it's easier to use functools.partial instead of itertools.repeat for the fixed arguments. Then you just zip up your variable arguments and pass them to a func_star that expects two fixed args, followed by a tuple of variable args:

import multiprocessing
from functools import partial

def func(a, b, c, d):
   print("{} {} {} {}".format(a, b, c, d))

def func_star(a, b, c_d):
   func(a, b, *c_d)

if __name__ == "__main__":
    pool = multiprocessing.Pool()
    partfunc = partial(func_star, '1', '2') # '1' and '2' are the fixed args
    var_list1 = ['a', 'b', 'c']
    var_list2 = ['x', 'y', 'z']
    pool.map(partfunc, zip(var_list1, var_list2))

Output:

1 2 a x
1 2 b y
1 2 c z
dano
  • 91,354
  • 19
  • 222
  • 219
0

You can try to use itertools.chain.from_iterable to flatten your lists.

Example (I am calling chain twice as you have nested lists..):

args = itertools.izip(('x', 'y'), itertools.repeat(('1', '2')))
itertools.chain.from_iterable( itertools.chain.from_iterable( args ) )
<itertools.chain object at 0x26F7E7F0>
list( _ )
['x', '1', '2', 'y', '1', '2']

Second example (not sure what exactly you want to achieve):

args = itertools.izip(('x', 'y'), itertools.repeat(('1', '2')))
[ list( itertools.chain.from_iterable( arg ) ) for arg in args ]
[['x', '1', '2'], ['y', '1', '2']]
pm007
  • 363
  • 1
  • 9
0

Why use itertools at all ?

>>> fixed = ("1", "2")
>>> variant = ("x", "y")
>>> argsgen = ((v,) + fixed for v in variant)
>>> list(argsgen)
[('x', '1', '2'), ('y', '1', '2')]
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118