469,290 Members | 1,887 Online

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

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

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.