While the suggested *fixes* are all correct (when in doubt, explicilty parenthesize!), none of the *explanations* for the unexpected output are quite right. Since this involves a subtle issue, I thought I'd send a full explanation. <br>
<br>It's tempting to assume that <span style="font-family: courier new,monospace;"><br>
<br>
>>> a in alist == b in alist</span><br>
is equivalent to <br>
<span style="font-family: courier new,monospace;">>>> ((a in alist) == b) in alist</span><br>
<br>
However, this is not correct!<br>
<br>
For a simpler (but perhaps more confusing) example of the hazards of
using "in" and "==", non-parenthesized, in an expression like this,
consider the following:<br>
<br><span style="font-family: courier new,monospace;">>>> a = 1 # as before</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">>>> alist = [5,6] # as before</span><br>
<span style="font-family: courier new,monospace;">>>> a in alist == False</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">False</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">>>> (a in alist) == False</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">True<br>
</span><br><span style="font-family: courier new,monospace;"></span>Weird, right? And no, putting parens around (alist == False) won't work--that would be an exception because the right side of the 'in' operator wouldn't be iterable.<br>
<br>
Here's one last example:<br>
<br>
<span style="font-family: courier new,monospace;">>>> blist = [1, [5,6]]</span><br>
<span style="font-family: courier new,monospace;">>>> 5 in alist == [5,6] in blist</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">True</span><br>
<br>
You might enjoy the exercise of figuring out why the above output is correct. But to cut to the chase, what's going on is the following. Python allows comparisons to be chained, as in the following:<br>
<br>
<span style="font-family: courier new,monospace;">>>> a == 1 == 2/2</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">True</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">>>> 1 < 5 < 7</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">True</span><br>
<br>
The way the chaining works (see 5.9 in <a href="http://tinyurl.com/3vsb6m">http://tinyurl.com/3vsb6m</a>) is that<br>
<br>
<span style="font-family: courier new,monospace;">>>> a == 1 == 2/2<br>
<span style="font-family: arial,helvetica,sans-serif;">is equivalent to </span><br>
</span><span style="font-family: courier new,monospace;">>>> (a == 1) and (1 == 2/2)</span><span style="font-family: courier new,monospace;"><br>
<span style="font-family: arial,helvetica,sans-serif;"><br>
and <br>
<br>
</span></span><span style="font-family: courier new,monospace;">>>> 1 < 5 < 7<br>
<span style="font-family: arial,helvetica,sans-serif;">is equivalent to</span><br style="font-family: courier new,monospace;">
</span><span style="font-family: courier new,monospace;">>>> (1 < 5) and (5 < 7)</span><span style="font-family: courier new,monospace;"></span><br>
<br>
Since 'in' is just another comparison operator, it works the same way. Thus, the the original expression<br>
<br>
<span style="font-family: courier new,monospace;">>>> a in alist == b in alist</span><br>
is equivalent to <br>
>>><span style="font-family: courier new,monospace;">(a in alist) and (alist == b) and (b in alist)</span><br>
<br>
which is <span style="font-family: courier new,monospace;">False</span> because all three comparisons are <span style="font-family: courier new,monospace;">False</span>. You'll see that the two other examples I came up with make sense in this context as well.<br>
<br>
-Damon<br><br><br><br><div class="gmail_quote">On Mon, Jan 25, 2010 at 1:27 PM, Asher Langton <span dir="ltr"><<a href="mailto:langton2@llnl.gov">langton2@llnl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">On Jan 25, 2010, at 1:12 PM, Max Slimmer wrote:<br>
</div><div><div></div><div class="h5"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Can anyone explain the following:<br>
<br>
>>> a = 1<br>
>>> b = 2<br>
>>> alist = [5,6]<br>
>>> print a in alist<br>
False<br>
<br>
>>> a in alist == b in alist<br>
False<br>
>>> a in alist == a in alist<br>
False<br>
>>> bool(a in alist) == bool(b in alist) # this does what we expect<br>
True<br>
>>> c = 5<br>
>>> c in alist == c in alist<br>
False<br>
>>><br>
</blockquote>
<br></div></div>
The '==' and 'in' operators have the same precedence, so the expression 'a in alist == b in alist' is evaluated left-to-right as:<br>
<br>
>>> ( (a in alist) == b) in alist<br>
<br>
Since 'a in alist' is False, this is the same as<br>
<br>
>>> ( False == b) in alist<br>
<br>
which can be simplified to<br>
<br>
>>> False in alist<br>
<br>
which is False.<br><font color="#888888">
<br>
<br>
-Asher</font><div><div></div><div class="h5"><br>
_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org" target="_blank">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
</div></div></blockquote></div><br>