[Tutor] misunderstanding "any"

Dave Angel d at davea.name
Wed Mar 7 19:11:39 CET 2012


On 03/07/2012 12:07 AM, col speed wrote:
> <snip>
> Then we have:
>
>
>>>> a = tuple(range(10))
>>>> b = tuple(reversed(a))
>>>> any(a) in b
> True
>
>>>> any(b) in a
> True
>
>>>> any((a,b)) in (a,b)
> False  # I think I understand this now, but I must admit it looks confusing!
>
> Thanks again
> Col

None of those last three does anything usefully similar to your original 
request.  Just because an English phrase makes sense, you can't expect 
the equivalent set of keywords to do anything remotely related.

If you insist on using the any function in solving your problem, you'll 
have to preprocess your two arrays into a single iterator.  And at that 
point, you might as well solve the problem in that loop.

(untested):

def test(a, b):
     for val1 in a:
         for val2 in b:
             if val1 == val2:  return True
     return False
print test(a,b)

Rather than:

def  findinter(a, b):
     res = []
     for val1 in a:
          for val2 in b:
               res.append(val1 == val2)
     return res

print any(findinter(a,b))



-- 

DaveA



More information about the Tutor mailing list