NOTE. Before commenting, read the BETTER UPDATE section below. There is some subtlety here. As far as I can tell, none of the answers above work in context.
I am trying to find an analog of the python map function with slightly different functionality. This is best explained by example. The map function performs the following actions:
In [1]: def f(x,y): return x+y In [2]: map(f, ['a','b','c'],['1','2','3']) Out[2]: ['a1', 'b2', 'c3']
So, the map turns f into a new function, let's call it f_zipper. f_zipper smooths its arguments and applies f to each archived pair.
What I would like to build is a function that I will call "magical_map", which behaves as follows:
In [1]: def f(x,y): return x+y In [2]: magical_map(f, ['a','b','c'],'1') Out[2]: ['a1', 'b1', 'c1']
So magical_map creates a bunch of calls to f (one for each item in the first argument list), but it collapses them all into the second argument.
Note. I need a really functional solution, so to speak, because I will not have access to the second argument.
i.e. what I'm going to do later, the following function is built:
intermed_func = functools.partial(magical_map, f) final_func = functools.partial(intermed_func, arg_I_know)
then final_func can be called
final_func(last_min_arg)
and return
[f(arg_I_know[0], last_min_arg), f(arg_I_know[1], last_min_arg), ...]
I am mostly obsessed with how to create a magical_map. Any help would be great. I was not lucky to find anything on this subject.
Thanks!
BEST UPDATE:
Solving a problem in context is much more complicated than just writing a function that works when both arguments are known at once. The problem is that they are not known in this context. More precisely, I need to do the following "final_func" apply split to all three lines. Right now, using "map", you get the following behavior.
def splitfunc(string, arg): return string.split(arg) intermed_func = functools.partial(map, splitfunc) final_func = functools.partial(intermed_func, ["a_b","v_c","g,g"]) final_func("_") Out[xx]: [['a', 'b'], ['v_c'], ['g,g']]
but when I define magical_map as suggested (by all means below), I get either errors or wrong behavior. For instance.
def magical_map(func, mylist, arg): return map(f, mylist, [arg]*len(mylist))
then I run:
intermed_func = functools.partial(magical_map, splitfunc) final_func = functools.partial(intermed_func, ["a_b","v,c","g,g"]) final_func("_")
I get:
['a_b_', 'v,c_', 'g,g_']