473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 1236

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
1
by: Xin Wang | last post by:
Some c++ guru said c++ is hard to learn but easy to use. Is python easy for both aspect? I found lot's of confused in coding. #code from Tkinter import * def on_click(m): print m def...
63
by: Stephen Thorne | last post by:
Hi guys, I'm a little worried about the expected disappearance of lambda in python3000. I've had my brain badly broken by functional programming in the past, and I would hate to see things...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
16
by: nephish | last post by:
Hey there, i have been learning python for the past few months, but i can seem to get what exactly a lamda is for. What would i use a lamda for that i could not or would not use a def for ? Is...
2
by: piuck | last post by:
I download IBM Synthetic Datat for Association rules(from http://www.almaden.ibm.com/cs/projects/iis/hdb/Projects/data_mining/datasets/syndata.html ), After I compiler it, it found a lot of bug......
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
1
by: Tim H | last post by:
Compiling with g++ 4: This line: if_then_else_return(_1 == 0, 64, _1) When called with a bignum class as an argument yields: /usr/include/boost/lambda/if.hpp: In member function 'RET...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.