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! 6 1237
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,[])
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,[])
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] = []
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] = []
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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......
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |