python: if and if statements

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

python: if and if statements

drfg2008
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

Reply | Threaded
Open this post in threaded view
|

Re: python: if and if statements

Garry Gelade
How about

1 not in [len(alpha), len(beta), len(beta2) ...]


Garry Gelade


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
drfg2008
Sent: 08 June 2011 08:12
To: [hidden email]
Subject: python: if and if statements

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-tp
4467945p4467945.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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
Reply | Threaded
Open this post in threaded view
|

Re: python: if and if statements

Albert-Jan Roskam
In reply to this post by drfg2008
http://docs.python.org/library/functions.html#all

>>> x = y = 2; z = 1
>>> [i != 1 for i in [x, y, z]]
[True, True, False]
>>> all([i != 1 for i in [x, y, z]])
False
 
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: drfg2008 <[hidden email]>
To: [hidden email]
Sent: Wed, June 8, 2011 9:12:00 AM
Subject: [SPSSX-L] python: if and if statements

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
[hidden email] (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
Reply | Threaded
Open this post in threaded view
|

Re: python: if and if statements

Jon K Peck
In reply to this post by drfg2008
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
[hidden email]
new phone: 720-342-5621




From:        drfg2008 <[hidden email]>
To:        [hidden email]
Date:        06/08/2011 01:17 AM
Subject:        [SPSSX-L] python: if and if statements
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




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
[hidden email] (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

Reply | Threaded
Open this post in threaded view
|

Re: python: if and if statements

Edwin Meijdam-2
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 <[hidden email]> 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
[hidden email]
new phone: 720-342-5621




From:        drfg2008 <[hidden email]>
To:        [hidden email]
Date:        06/08/2011 01:17 AM
Subject:        [SPSSX-L] python: if and if statements
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




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
[hidden email] (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
[hidden email]
http://www.linkedin.com/in/emeijdam

Reply | Threaded
Open this post in threaded view
|

Re: python: if and if statements

Albert-Jan Roskam
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 <[hidden email]>
To: [hidden email]
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 <[hidden email]> 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
[hidden email]
new phone: 720-342-5621




From:        drfg2008 <[hidden email]>
To:        [hidden email]
Date:        06/08/2011 01:17 AM
Subject:        [SPSSX-L] python: if and if statements
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




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
[hidden email] (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
[hidden email]
http://www.linkedin.com/in/emeijdam

Reply | Threaded
Open this post in threaded view
|

Re: python: if and if statements

Jon K Peck
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
[hidden email]
new phone: 720-342-5621




From:        Albert-Jan Roskam <[hidden email]>
To:        [hidden email]
Date:        06/08/2011 10:03 AM
Subject:        Re: [SPSSX-L] python: if and if statements
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




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 <[hidden email]>
To:
[hidden email]
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@...> 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@...
new phone: 720-342-5621





From:        
drfg2008 <kontakt@...>
To:        
[hidden email]
Date:        06/08/2011 01:17 AM
Subject:        
[SPSSX-L] python: if and if statements
Sent by:        
"SPSSX(r) Discussion" <[hidden email]>





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@... (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@...
http://www.linkedin.com/in/emeijdam

Reply | Threaded
Open this post in threaded view
|

Re: python: if and if statements

Albert-Jan Roskam
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 <[hidden email]>
To: [hidden email]
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
[hidden email]
new phone: 720-342-5621




From:        Albert-Jan Roskam <[hidden email]>
To:        [hidden email]
Date:        06/08/2011 10:03 AM
Subject:        Re: [SPSSX-L] python: if and if statements
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




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 <[hidden email]>
To:
[hidden email]
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 <[hidden email]> 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

[hidden email]
new phone: 720-342-5621





From:        
drfg2008 <[hidden email]>
To:        
[hidden email]
Date:        06/08/2011 01:17 AM
Subject:        
[SPSSX-L] python: if and if statements
Sent by:        
"SPSSX(r) Discussion" <[hidden email]>





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

[hidden email] (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

[hidden email]
http://www.linkedin.com/in/emeijdam

Reply | Threaded
Open this post in threaded view
|

Re: python: if and if statements

Jon K Peck
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
[hidden email]
new phone: 720-342-5621




From:        Albert-Jan Roskam <[hidden email]>
To:        Jon K Peck/Chicago/IBM@IBMUS, [hidden email]
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 <[hidden email]>
To:
[hidden email]
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
[hidden email]
new phone: 720-342-5621





From:        
Albert-Jan Roskam <[hidden email]>
To:        
[hidden email]
Date:        
06/08/2011 10:03 AM
Subject:        
Re: [SPSSX-L] python: if and if statements
Sent by:        
"SPSSX(r) Discussion" <[hidden email]>




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 <[hidden email]>
To:
[hidden email]
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@...> 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@...
new phone: 720-342-5621





From:        
drfg2008 <kontakt@...>
To:        
[hidden email]
Date:        
06/08/2011 01:17 AM
Subject:        
[SPSSX-L] python: if and if statements
Sent by:        
"SPSSX(r) Discussion" <[hidden email]>





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@... (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@...
http://www.linkedin.com/in/emeijdam

Reply | Threaded
Open this post in threaded view
|

Re: python: if and if statements

drfg2008
In reply to this post by Jon K Peck
Thanks everyone for your help.

However, for a python-starter like me there is a little problem with:

all([len(item) == 1 for item in [a,b,c]])


I tried:

     if all([len(item) == 1 for item in [alpha,beta1,beta2]):
           .... code

     all([len(item) == 1 for item in [alpha,beta1,beta2]):
           .... code

     all([len(item) == 1 for item in [alpha,beta1,beta2])
           .... code


no success. I checked list comprehensions in python, but couldn't find a similar case so far.

Thanks for help.


Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: python: if and if statements

Albert-Jan Roskam
You'll usually find a list comprehension in something like the following form
items = [a,b,c]
[len(item) == 1 for item in items] # the list comprehension
The part "len(item) == 1" is evaluated for each item, and evaluates to True or False.
The resulting lists consists of Booleans, which are given to the all() function. If all the booleans in the lists are True, all() returns True, else it returns False.
 
So if you subdivide the one-liner into two or three steps, the code is easier to understand. It also allows you to insert some print statements to see what's going on.
 
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: drfg2008 <[hidden email]>
To: [hidden email]
Sent: Thu, June 9, 2011 10:01:26 AM
Subject: Re: [SPSSX-L] python: if and if statements

Thanks everyone for your help.

However, for a python-starter like me there is a little problem with:

all([len(item) == 1 for item in [a,b,c]])


I tried:

    if all([len(item) == 1 for item in [alpha,beta1,beta2]):
          .... code

    all([len(item) == 1 for item in [alpha,beta1,beta2]):
          .... code

    all([len(item) == 1 for item in [alpha,beta1,beta2])
          .... code


no success. I checked list comprehensions in python, but couldn't find a
similar case so far.

Thanks for help.




-----
Dr. Frank Gaeth
FU-Berlin

--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/python-if-and-if-statements-tp4467945p4471929.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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
Reply | Threaded
Open this post in threaded view
|

Re: python: if and if statements

Garry Gelade
In reply to this post by drfg2008
You have unbalanced square brackets in your example.

Garry Gelade

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
drfg2008
Sent: 09 June 2011 09:01
To: [hidden email]
Subject: Re: python: if and if statements

Thanks everyone for your help.

However, for a python-starter like me there is a little problem with:

all([len(item) == 1 for item in [a,b,c]])


I tried:

     if all([len(item) == 1 for item in [alpha,beta1,beta2]):
           .... code

     all([len(item) == 1 for item in [alpha,beta1,beta2]):
           .... code

     all([len(item) == 1 for item in [alpha,beta1,beta2])
           .... code


no success. I checked list comprehensions in python, but couldn't find a
similar case so far.

Thanks for help.




-----
Dr. Frank Gaeth
FU-Berlin

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/python-if-and-if-statements-tp
4467945p4471929.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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
Reply | Threaded
Open this post in threaded view
|

Re: python: if and if statements

drfg2008
In reply to this post by drfg2008
ups, unbalanced square brackets


if all([len(item) == 1 for item in [alpha,beta1,beta2]]):


need to get some new glasses, thanks!
Dr. Frank Gaeth