I have this case where I have a list of lists of lists, and I need to apply a reduce on each of the sub lists of the first list. The reduce function requires 2 parameters, but that 2nd parameter (the list of lists I want to apply the reduce on), is supposed to be from the main list I pass to the map function Consider the snippet below.
reducedLists = map(reduce(lambda first, second : map(operator.add, first,second), XXX), listsToReduce)
I need to know what should be passed in place of XXX
above.
Here listsToReduce
is a list of lists of lists like [[[1,2,3], [3,2,1]],[[1,3,5],[5,3,1]]]
.
I want the final output of the above map and reduce to be 2 lists
[[4,4,4],[6,6,6]]
which is a pairwise sum of the inner list of list passed to the map.
I could not figure how I can model the map and reduce to pass proper parameters to the reduce function.
My ultimate objective is to use Pool.map
from the multiprocessing
package to perform the reduce operations on multiple cores. Any inputs on restructuring the code with this in mind are much appreciated.
I'd perform that pairwise summing operation this way:
listsToReduce = [[[1,2,3], [3,2,1]], [[1,3,5], [5,3,1]]]
reducedLists = [list(map(sum, zip(*lst))) for lst in listsToReduce]
print(reducedLists)
output
[[4, 4, 4], [6, 6, 6]]
In Python 2, map
returns a list, so you could do
[map(sum, zip(*lst)) for lst in listsToReduce]
but I strongly recommend the use of Python 3 for all new code, since Python 2 reaches its official End of Life in 2020.
Here's another way to do it in Python 2, but it's less efficient due to the use of the lambda
function instead of sum
.
from operator import add
reducedLists = map(lambda t: map(add, *t), listsToReduce)