[Tutor] misunderstanding "any"

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Mar 7 05:50:53 CET 2012


On 07/03/2012 04:36, col speed wrote:
> On 7 March 2012 10:45, Mark Lawrence<breamoreboy at yahoo.co.uk>  wrote:
>> On 07/03/2012 03:24, col speed wrote:
>>>
>>> Hello again
>>> Hope you are all well.
>>>
>>> I'm trying to make a "match 3" game, where you have a square grid and
>>> have to put 3 matching shapes in a row.
>>> I need a function that tells me if the game is playable, ie. it is
>>> possible to match 3 shapes by only swapping 2 adjacent shapes.
>>> I have looked at the co-ordinates and got a list of the "offset
>>> co-ordinates" needed for the above.
>>>
>>> I have a list of coordinates and a list of "lemons" and I want to see
>>> if *any* lemon coordinate is in the list of coordinates.
>>> I tried this:
>>> if any(((x+1, y+1), (x-1, y+2),(x-2, y+1),(x-1, y-1 ))) in fruit_type:
>>>                      return True
>>>
>>> Thinking that if  *any* of the tuples is in fruit_type(a list of
>>> tuples), then it should return True.
>>> However, it always equates to False.
>>>
>>
>>
>> Here's the way to find out.
>>
>>>>> help(any)
>> Help on built-in function any in module __builtin__:
>>
>> any(...)
>>     any(iterable) ->  bool
>>
>>     Return True if bool(x) is True for any x in the iterable.
>>
>>>>> help('in')
>> Comparisons
>> ***********
>>
>> [snipped]
>>
>>
>>
>> For the list and tuple types, ``x in y`` is true if and only if there
>> exists an index *i* such that ``x == y[i]`` is true.
>>
>>
>> [snipped]
>>
>> --
>> Cheers.
>>
>> Mark Lawrence.
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
> Thanks Mark
> I looked up help(any), but not help(in)!

Note as it's a keyword it has to be in quotes, help('in').

>
> I *think* I understand:
> Where it says:
> "For the list and tuple types, ``x in y`` is true if and only if there
>> exists an index *i* such that ``x == y[i]`` is true."
>
> I suppose I am looking for .....an index *i* and *j* such that x[j] == y[i].
>
> Is that right?

I reckon so although I don't believe that the interactive prompt lies.

 >>> a=tuple(range(10))
 >>> b=tuple(reversed(a))
 >>> a,b
((0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
 >>> a[3] == b[3]
False
 >>> a[5] == b[4]
True

> cheers
> Col
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

HTH.

-- 
Cheers.

Mark Lawrence.



More information about the Tutor mailing list