Date: Wed, 8 Jun 2011 11:08:14 -0600
Reply-To: Jon K Peck <peck@us.ibm.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Jon K Peck <peck@us.ibm.com>
Subject: Re: python: if and if statements
In-Reply-To: <339733.32845.qm@web110710.mail.gq1.yahoo.com>
Content-Type: multipart/alternative;
IMO, map, filter, et al are redundant and no longer recommended. There
are a few occasions where I still find reduce useful. The list
comprehension provides a general solution that covers all of these. List
comprehensions are more efficient than explicit loops, too. They generate
fewer virtual machine instructions.
R looping is notoriously slow, so using vectorized functions is always
preferred there. I just wish that it didn't have so many different
apply-like functions, since it is hard to keep them all straight (apply,
sapply, mapply, lapply, tapply). That's a symptom of a badly designed
language IMO.
Jon Peck
Senior Software Engineer, IBM
peck@us.ibm.com
new phone: 720-342-5621
From: Albert-Jan Roskam <fomcl@yahoo.com>
To: Jon K Peck/Chicago/IBM@IBMUS, SPSSX-L@LISTSERV.UGA.EDU
Date: 06/08/2011 10:45 AM
Subject: Re: [SPSSX-L] python: if and if statements
That's an even nicer notation. Yes, I've heard that many people are not
fans of lambda, or of map, filter, reduce. Lambda is nice and compact but
I could live with just normal def functions. I find it useful, though, to
pass an argument to an event handler, but now that I have looked up an
example I see that there are alternatives:
http://stackoverflow.com/questions/3296893/how-to-pass-an-argument-to-event-handler-python-tkinter-programming
(I meant the second solution)
I read somewhere that loops could be faster when map is used, compared to
a simple for loop, because the code 'is pushed straight into C'. Is that
true? In R it certainly is (*apply vs. for) but in Python..?
Cheers!!
Albert-Jan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine,
public order, irrigation, roads, a fresh water system, and public health,
what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: Jon K Peck <peck@us.ibm.com>
To: SPSSX-L@LISTSERV.UGA.EDU
Sent: Wed, June 8, 2011 6:09:57 PM
Subject: Re: [SPSSX-L] python: if and if statements
No need for lambda here or filter - and, BTW, lambda barely escaped
elimination in Python 3.
All you need is a list comprehension.
all([len(item) == 1 for item in [a,b,c]])
Jon Peck
Senior Software Engineer, IBM
peck@us.ibm.com
new phone: 720-342-5621
From: Albert-Jan Roskam <fomcl@yahoo.com>
To: SPSSX-L@LISTSERV.UGA.EDU
Date: 06/08/2011 10:03 AM
Subject: Re: [SPSSX-L] python: if and if statements
Sent by: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
Hmm, I like this better:
>>> all([len(alpha)==1, len(beta)==1, len(beta2)==1,
len(alpha_stdfehler)==1])
True
But you're right, lambdas rock:
>>> all([filter(lambda arg: len(arg)==1, [alpha, beta, beta2,
alpha_stdfehler])])
True
Cheers!!
Albert-Jan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine,
public order, irrigation, roads, a fresh water system, and public health,
what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: Edwin Meijdam <emeijdam@DASC.NL>
To: SPSSX-L@LISTSERV.UGA.EDU
Sent: Wed, June 8, 2011 3:29:01 PM
Subject: Re: [SPSSX-L] python: if and if statements
Probably a bit too much, but I just like map & lambda functions:
Assuming the vars are strings:
alpha = 't'
beta = 'e'
beta2 = 's'
alpha_stdfehler = 't'
aList = [alpha, beta, beta2, alpha_stdfehler]
allOneLenght = sum(list(map(lambda x: len(x) == 1, aList))) == len(aList)
if allOneLenght:
Hope this help (but I doubt that...),
Edwin Meijdam
On Wed, Jun 8, 2011 at 2:42 PM, Jon K Peck <peck@us.ibm.com> wrote:
Lots of ways to do this. Here's one more
if min(a,b,c) == max(a,b,c) == 1:
...
IIRC, the original logic was just that these values not be 0, so you could
simplify further to
if min(a,b,c):
...
since 0 is False
Jon Peck
Senior Software Engineer, IBM
peck@us.ibm.com
new phone: 720-342-5621
From: drfg2008 <kontakt@datest.de>
To: SPSSX-L@LISTSERV.UGA.EDU
Date: 06/08/2011 01:17 AM
Subject: [SPSSX-L] python: if and if statements
Sent by: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
how can I cut short lots of "if and if ..."-Statements in python (all
statements have to be ==1). Example:
if len(alpha) ==1 and len(beta1) ==1 and len(beta2) ==1 and
len(alpha_stdfehler) ==1 and len(beta1_stdfehler) ==1 and
len(beta2_stdfehler) ==1 and len(alpha_T)==1 and len(beta1_T)==1 and
len(beta2_T)==1 and len(r_quadrat)==1 and len(r_quadrat_stdfehler)==1 :
-> if any(alpha, beta, ....) <> 1
continue
else: ...
something like that? Couldn't find a solution.
Thanks
-----
Dr. Frank Gaeth
FU-Berlin
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/python-if-and-if-statements-tp4467945p4467945.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.
=====================
To manage your subscription to SPSSX-L, send a message to
LISTSERV@LISTSERV.UGA.EDU (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
--
Edwin Meijdam
+31(0)6 159 608 37
DASC B.V.
Postbus 88 4130 EB Vianen
085 - 8774406
emeijdam@dasc.nl
http://www.linkedin.com/in/emeijdam
[text/html]