[Tutor] misunderstanding "any"

col speed ajarncolin at gmail.com
Thu Mar 8 00:53:05 CET 2012


On 8 March 2012 01:11, Dave Angel <d at davea.name> wrote:
> 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
>
Thanks again to everybody for your very clear explanations.
I have done away with any and have defined a function that iterates
through one list and returns True if *i* in the next .
It seems to work every time and also looks a little clearer.
Cheers
Col


More information about the Tutor mailing list