External variables in lambda functions in Python
Inspired by the construction in this answer, I am trying to do the following:
values = range(3)
vector = np.random.randint(3, size=(5,))
f = lambda x: x in values
result = [f(a) for a in values]
but I get global name 'values' is not defined.
I get the same error if I try the solution that I linked to above, i.e.:
A = [[0,1,2], [1,2,3], [2,3,4]]
v = [1,2]
B = [map(lambda val: val in v) for a in A]
Did Python change since that solution was posted? (I am working with
2.7.4). If so, how can I access an external variable within a lambda
function? Should I declare it as global? pass it as another input?
Update 1:
I am only noticing this problem within an embedded shell in IPython (1.0).
I will keep investigating
Update 2:
I am unable reproduce the problem at the moment. I will look deeper into
it and update/close accordingly.
No comments:
Post a Comment