compare list

Ben Bush pythonnew at gmail.com
Tue Nov 15 02:24:10 EST 2005


>
>  Hi Brian,
regarding "if item + 1 in list1 and item + 1 in list2:",
my understanding is this will check whether the following item in each list
is the same. How does the code permit the situation that the order does not
matter?
For example, for lisA and lisB, the comparison is true and the two lists
have 5 and 6 but different order.
 lisA=[1,2,3,4,5,6,9]
lisB=[1,6,5]
 Many Thanks!

 Unfortunately, the indents got screwed up along the way. But the part
> of my code you asked about was:
>
> for item in list1:
> if item in list2:
> if item + 1 in list1 and item + 1 in list2:
> return True
>
> In some detail:
>
> Consider each item in list1 in turn. If the item isn't in list2, move
> on, as there is no chance of it meeting your test. If the item is in
> list2, then it is pointful to check if your test is met. Given that
> you wanted consecutive numbers, the 'if item + 1 in list1 and item + 1
> in list2' condition checks if both lists (which contain item if we've
> gone this far) also contain item + 1. If they do, you wanted the
> function to return 1 (I changed it to True as more idiomatic). After
> my for loop is a return False which we will only hit if the condition
> you wanted to test was never satisfied.
>
> Does that clarify it?
>
> Finally, your response to Alex would have been much more useful if
> you'd quoted the error rather than just asserting that you got an
> error :-)
>
> Best,
>
> Brian vdB
>
>
>


--
Thanks!
Ben Bush
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051114/06f780fe/attachment.html>


More information about the Python-list mailing list