Re: [Pythonmac-SIG] If/else vs or

2010-10-29 Thread Dan Ross


I've been trying to use more list comprehensions recently. 

I was just
fleshing something out which brought on my post. 

On Fri, 29 Oct 2010
09:15:47 -0400, Henry Olders  wrote: When dealing with lists, list
comprehensions are shorter and easier to work with:
l=['red','green','orange','blue','red','white']  [x for x in l if x in
['red','blue']] 
 Henry  

  On 2010-10-28, at 10:21 , Dan Ross wrote:


I don't think this is Mac specific, but I wonder if someone could explain
why these two groups of code behave differently: 

[code] 

colors =
['red', 'green', 'blue', 'orange', 'fuscia', 'black',
'white']

list_of_matches = []
for x in colors:
 if x == 'red' or 'green'
or 'blue':
 list_of_matches.append(x)
print
list_of_matches

list_of_matches2 = []
for x in colors:
 if x == 'red':

list_of_matches2.append(x)
 elif x == 'green':
 list_of_matches2.append(x)

elif x == 'blue':
 list_of_matches2.append(x)
 else:
 pass
print
list_of_matches2 

[/code] 

list_of_matches contains every item in colors.


list_of_matches2 only contains the matches, as I would expect. 

I don't
get it.. 

Thanks, 

Dan  

 ___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG


Re: [Pythonmac-SIG] If/else vs or

2010-10-29 Thread Christopher Barker

On 10/29/10 7:56 AM, Dan Ross wrote:
 I've been trying to use more list comprehensions recently.

ahh -- then you want something like:

In [15]: colors = ['red','green','blue','orange','fuchsia','black','white']

In [16]: subset = ['red','green','blue','purple']

In [17]: [c for c in colors if c in subset]

Out[17]: ['red', 'green', 'blue']


(so much for one obvious way to do it!)

-Chris


--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/ORR(206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115   (206) 526-6317   main reception

chris.bar...@noaa.gov
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG


Re: [Pythonmac-SIG] If/else vs or

2010-10-29 Thread Dan Ross
Indeed. That's awfully nice and concise.

On Fri, 29 Oct 2010 09:14:06 -0700, Christopher Barker
chris.bar...@noaa.gov wrote:
 On 10/29/10 7:56 AM, Dan Ross wrote:
   I've been trying to use more list comprehensions recently.
 
 ahh -- then you want something like:
 
 In [15]: colors =
['red','green','blue','orange','fuchsia','black','white']
 
 In [16]: subset = ['red','green','blue','purple']
 
 In [17]: [c for c in colors if c in subset]
 
 Out[17]: ['red', 'green', 'blue']
 
 
 (so much for one obvious way to do it!)
 
 -Chris
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG


Re: [Pythonmac-SIG] If/else vs or

2010-10-29 Thread Chris Weisiger
Or you could use actual sets:

 colors = set(['red', 'green', 'blue', 'orange', 'fuscia', 'black',
'white'])
 subset = set(['red', 'green', 'blue', 'purple'])
 subset.intersection(colors)
set(['blue', 'green', 'red'])

Of course, this loses your ordering, but it's otherwise far easier to read
than a list comprehension.

-Chris

On Fri, Oct 29, 2010 at 9:19 AM, Dan Ross d...@rosspixelworks.com wrote:

 Indeed. That's awfully nice and concise.

 On Fri, 29 Oct 2010 09:14:06 -0700, Christopher Barker
 chris.bar...@noaa.gov wrote:
  On 10/29/10 7:56 AM, Dan Ross wrote:
I've been trying to use more list comprehensions recently.
 
  ahh -- then you want something like:
 
  In [15]: colors =
 ['red','green','blue','orange','fuchsia','black','white']
 
  In [16]: subset = ['red','green','blue','purple']
 
  In [17]: [c for c in colors if c in subset]
 
  Out[17]: ['red', 'green', 'blue']
 
 
  (so much for one obvious way to do it!)
 
  -Chris
 ___
 Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
 http://mail.python.org/mailman/listinfo/pythonmac-sig
 unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG

___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG


[Pythonmac-SIG] If/else vs or

2010-10-28 Thread Dan Ross


I don't think this is Mac specific, but I wonder if someone could
explain why these two groups of code behave differently: 

[code] 

colors
= ['red', 'green', 'blue', 'orange', 'fuscia', 'black',
'white']

list_of_matches = []
for x in colors:
 if x == 'red' or 'green'
or 'blue':
 list_of_matches.append(x)
print
list_of_matches

list_of_matches2 = []
for x in colors:
 if x == 'red':

list_of_matches2.append(x)
 elif x == 'green':
 list_of_matches2.append(x)

elif x == 'blue':
 list_of_matches2.append(x)
 else:
 pass
print
list_of_matches2 

[/code] 

list_of_matches contains every item in colors.


list_of_matches2 only contains the matches, as I would expect. 

I don't
get it.. 

Thanks, 

Dan___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG


Re: [Pythonmac-SIG] If/else vs or

2010-10-28 Thread Ronald Oussoren

On 28 Oct, 2010, at 16:21, Dan Ross wrote:

 I don't think this is Mac specific, but I wonder if someone could explain why 
 these two groups of code behave differently:
 
 [code]
 
 colors = ['red', 'green', 'blue', 'orange', 'fuscia', 'black', 'white']
 
 list_of_matches = []
 for x in colors:
 if x == 'red' or 'green' or 'blue':
 

This parses as:

if x == ('red' or 'green' or blue'):

Ronald

smime.p7s
Description: S/MIME cryptographic signature
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG


Re: [Pythonmac-SIG] If/else vs or

2010-10-28 Thread Zachary Pincus


On Oct 28, 2010, at 11:08 AM, Ronald Oussoren wrote:



On 28 Oct, 2010, at 16:21, Dan Ross wrote:

I don't think this is Mac specific, but I wonder if someone could  
explain why these two groups of code behave differently:


[code]

colors = ['red', 'green', 'blue', 'orange', 'fuscia', 'black',  
'white']


list_of_matches = []
for x in colors:
if x == 'red' or 'green' or 'blue':



This parses as:

if x == ('red' or 'green' or blue'):


This would always lead to the if-test failing: ('red' or 'green' or  
'blue') evaluates to True, and x != True. What's observed is the if- 
test always passing... As the equality operator is higher-precedence  
than boolean operators, and equal precedence operators group left-to- 
right, the above parses as:

if (((x == 'red') or 'green') or 'blue'):

noting that non-empty strings (like 'green') evaluate as True in an if- 
test, this will test if x == 'red', and if not, it will go on to  
testing if 'green' evaluates to True (which it does), and so forth.


Dan, you could fix your code as:
if x == 'red' or x == 'green' or x == 'blue':

But this is better:
if x in ('red', 'green', 'blue'):

and this scales best:
good_colors = set(['red', 'green', 'blue'])
if x in good_colors:


Zach







Ronald
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG


___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG


Re: [Pythonmac-SIG] If/else vs or

2010-10-28 Thread Richard Fuhr
I have attached a copy of the originally-posted Python code and also have
attached an IDLE session based on that code, which seems instructive.
(Copying and pasting the IDLE session into the email message seems to mess
up the indentation.)
But Zachary's suggestions for rewriting the original Python to attain the
intended result are very helpful.

On Thu, Oct 28, 2010 at 8:20 AM, Zachary Pincus zachary.pin...@yale.eduwrote:


 On Oct 28, 2010, at 11:08 AM, Ronald Oussoren wrote:


 On 28 Oct, 2010, at 16:21, Dan Ross wrote:

  I don't think this is Mac specific, but I wonder if someone could explain
 why these two groups of code behave differently:

 [code]

 colors = ['red', 'green', 'blue', 'orange', 'fuscia', 'black', 'white']

 list_of_matches = []
 for x in colors:
if x == 'red' or 'green' or 'blue':


 This parses as:

if x == ('red' or 'green' or blue'):


 This would always lead to the if-test failing: ('red' or 'green' or 'blue')
 evaluates to True, and x != True. What's observed is the if-test always
 passing... As the equality operator is higher-precedence than boolean
 operators, and equal precedence operators group left-to-right, the above
 parses as:

if (((x == 'red') or 'green') or 'blue'):

 noting that non-empty strings (like 'green') evaluate as True in an
 if-test, this will test if x == 'red', and if not, it will go on to testing
 if 'green' evaluates to True (which it does), and so forth.

 Dan, you could fix your code as:
if x == 'red' or x == 'green' or x == 'blue':

 But this is better:
if x in ('red', 'green', 'blue'):

 and this scales best:
good_colors = set(['red', 'green', 'blue'])
if x in good_colors:


 Zach






  Ronald
 ___
 Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
 http://mail.python.org/mailman/listinfo/pythonmac-sig
 unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG


 ___
 Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
 http://mail.python.org/mailman/listinfo/pythonmac-sig
 unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG



Oct28.py
Description: Binary data


IdleSessionOct28
Description: Binary data
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG


Re: [Pythonmac-SIG] If/else vs or

2010-10-28 Thread Dan Ross
Thank you for your help guys.

Zach, I appreciate the explanation. That's what I was looking for.

Dan

Part 3
Description: boundary/apple-mail-7-733662729
___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG


Re: [Pythonmac-SIG] If/else vs or

2010-10-28 Thread Daniel O'Donovan

On 28 Oct 2010, at 15:21, Dan Ross wrote:

 if x == 'red' or 'green' or 'blue':
 
 if x == 'red' or 'green' or 'blue':

I think your logic might need straightening here, you're saying

if (x == 'red') 
or 
if 'green' 
or 
if 'blue'


but I think you mean

if (x == 'red') 
or 
if (x == 'green') 
or 
if (x == 'blue')

so try this

if (x == 'red') or (x == 'green') or (x == 'blue'):

or maybe even

if x in ('red', 'green', 'blue'):

*oops* I see Ronald has got his answer in first!


HTH 

Dan

Daniel O'Donovan
dan.odono...@gmail.com



___
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG