Pergunta

I'm trying to figure out the way to properly use map in python so that I can multi-thread my program by Pool.map. Basically I'm running into issues trying to understand how functional python works. I have:

import numpy as np

def maptest(foo,bars):
   print foo * bars

main():
   matA = np.eye(2)
   matB = np.eye(2)

   print map((lambda foo: maptest(foo, matB)), matA) 

This gives me an output of:

[[ 1.  0.]
 [ 0.  0.]]
[[ 0.  0.]
 [ 0.  1.]]
[None, None]

when the output I want is simply:

[[1. 0.]
 [0. 1.]]

What's going on with the map call I can? This is my first time using map and lambda. I've used lambdify with sympy, but that's all for my functionals experience. Thanks!

Foi útil?

Solução

The [None, None] is coming from printing the map call (note that your maptest function prints!).

Now, the reason that it prints those multiple arrays is that you are mapping your function across all of mapA. mapA is actually a two-element array, and map applies your function to each element of the array. Hence, you print [1,0][[1,0][0,1]], then [0,1][[1,0][0,1]]. Instead of multiplying the matrices, you have made two multiplications, one for each element of mapA.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top