469,290 Members | 1,887 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

about lambda

what does the following code mean? It is said to be used in the
calculation of the overlaid area size between two polygons.
map(lambda x:b.setdefault(x,[]),a)

Thanks!
Nov 22 '05 #1
6 1158

Shi Mu wrote:
what does the following code mean? It is said to be used in the
calculation of the overlaid area size between two polygons.
map(lambda x:b.setdefault(x,[]),a)


The equivalent of :

def oh_my_yet_another_function_name_why_not_use_lambda (x):
b.setdefault(x,[])

map(oh_my_yet_another_function_name_why_not_use_la mbda, a)

Or

for x in a:
b.setdefault(x,[])

Nov 22 '05 #2

Shi Mu wrote:
what does the following code mean? It is said to be used in the
calculation of the overlaid area size between two polygons.
map(lambda x:b.setdefault(x,[]),a)


The equivalent of :

def oh_my_yet_another_function_name_why_not_use_lambda (x):
b.setdefault(x,[])

map(oh_my_yet_another_function_name_why_not_use_la mbda, a)

Or

for x in a:
b.setdefault(x,[])

Nov 22 '05 #3
Shi Mu wrote:
what does the following code mean? It is said to be used in the
calculation of the overlaid area size between two polygons.
map(lambda x:b.setdefault(x,[]),a)

Thanks!


Assuming b is a dict, it is roughly equivalent to the following (except
that the variables beginning with _ don't exist):

_result = []
for _key in a:
if _key not in b:
b[_key] = []
_result.append(b[_key])

A more usual way to write this would be:

result = [b.setdefault(key, []) for key in a]

I added an assignment because I'm assuming that even though you didn't show
it the original expression made some use of the resulting list, otherwise
it is just wasting time and effort obfuscating something which could be
more simply written as:

for key in a:
if key not in b:
b[key] = []
Nov 22 '05 #4
Shi Mu wrote:
what does the following code mean? It is said to be used in the
calculation of the overlaid area size between two polygons.
map(lambda x:b.setdefault(x,[]),a)

Thanks!


Assuming b is a dict, it is roughly equivalent to the following (except
that the variables beginning with _ don't exist):

_result = []
for _key in a:
if _key not in b:
b[_key] = []
_result.append(b[_key])

A more usual way to write this would be:

result = [b.setdefault(key, []) for key in a]

I added an assignment because I'm assuming that even though you didn't show
it the original expression made some use of the resulting list, otherwise
it is just wasting time and effort obfuscating something which could be
more simply written as:

for key in a:
if key not in b:
b[key] = []
Nov 22 '05 #5
"bo****@gmail.com" <bo****@gmail.com> wrote in
news:11**********************@g14g2000cwa.googlegr oups.com:

Shi Mu wrote:
what does the following code mean? It is said to be used in the
calculation of the overlaid area size between two polygons.
map(lambda x:b.setdefault(x,[]),a)


The equivalent of :

def oh_my_yet_another_function_name_why_not_use_lambda (x):
b.setdefault(x,[])

map(oh_my_yet_another_function_name_why_not_use_la mbda, a)

Or

for x in a:
b.setdefault(x,[])


Or even:
[b.setdefault(x,[]) for x in a]

The effect of the code is this: if you have b, a dictionary of
values, and a, a list or tuple of indexes to the dictionary, you
can generate a list that will contain just the values associated
with the indices in the list. If the index is not found in the
dictionary, the default value will be used; in this case, that is
an empty list.

So, for example, if you have
b = {'x':1,1:(1,2,3),'arthur':'A string',99:{'j':45,'k':111}}
and a looks like this: you produce this:
a = (0,1,'x') [[], (1, 2, 3), 1]
a = (0,2,3,22) [[], [], [], []]
a = ['x','arthur'] [1, 'A string']

.... and so on.
--
rzed
Nov 22 '05 #6
"bo****@gmail.com" <bo****@gmail.com> wrote in
news:11**********************@g14g2000cwa.googlegr oups.com:

Shi Mu wrote:
what does the following code mean? It is said to be used in the
calculation of the overlaid area size between two polygons.
map(lambda x:b.setdefault(x,[]),a)


The equivalent of :

def oh_my_yet_another_function_name_why_not_use_lambda (x):
b.setdefault(x,[])

map(oh_my_yet_another_function_name_why_not_use_la mbda, a)

Or

for x in a:
b.setdefault(x,[])


Or even:
[b.setdefault(x,[]) for x in a]

The effect of the code is this: if you have b, a dictionary of
values, and a, a list or tuple of indexes to the dictionary, you
can generate a list that will contain just the values associated
with the indices in the list. If the index is not found in the
dictionary, the default value will be used; in this case, that is
an empty list.

So, for example, if you have
b = {'x':1,1:(1,2,3),'arthur':'A string',99:{'j':45,'k':111}}
and a looks like this: you produce this:
a = (0,1,'x') [[], (1, 2, 3), 1]
a = (0,2,3,22) [[], [], [], []]
a = ['x','arthur'] [1, 'A string']

.... and so on.
--
rzed
Nov 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

220 posts views Thread by Brandon J. Van Every | last post: by
28 posts views Thread by David MacQuigg | last post: by
53 posts views Thread by Oliver Fromme | last post: by
1 post views Thread by Xin Wang | last post: by
63 posts views Thread by Stephen Thorne | last post: by
23 posts views Thread by Kaz Kylheku | last post: by
16 posts views Thread by nephish | last post: by
21 posts views Thread by globalrev | last post: by
1 post views Thread by Tim H | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.