[Tutor] misunderstanding "any"

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Mar 7 04:45:36 CET 2012


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.
> If I iterate through the tuples and see if any are in fruit_type, it
> returns True (when it should).
> I have tried many print statements to make sure what is happening, and
> also to make sure that I am comparing type<tuples>.

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]

The operators ``in`` and ``not in`` test for collection membership.
``x in s`` evaluates to true if *x* is a member of the collection *s*,
and false otherwise.  ``x not in s`` returns the negation of ``x in
s``. The collection membership test has traditionally been bound to
sequences; an object is a member of a collection if the collection is
a sequence and contains an element equal to that object.  However, it
make sense for many other object types to support membership tests
without being a sequence.  In particular, dictionaries (for keys) and
sets support membership testing.

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.

For the Unicode and string types, ``x in y`` is true if and only if
*x* is a substring of *y*.  An equivalent test is ``y.find(x) != -1``.
Note, *x* and *y* need not be the same type; consequently, ``u'ab' in
'abc'`` will return ``True``. Empty strings are always considered to
be a substring of any other string, so ``"" in "abc"`` will return
``True``.

Changed in version 2.3: Previously, *x* was required to be a string of
length ``1``.

For user-defined classes which define the ``__contains__()`` method,
``x in y`` is true if and only if ``y.__contains__(x)`` is true.

For user-defined classes which do not define ``__contains__()`` but do
define ``__iter__()``, ``x in y`` is true if some value ``z`` with ``x
== z`` is produced while iterating over ``y``.  If an exception is
raised during the iteration, it is as if ``in`` raised that exception.

Lastly, the old-style iteration protocol is tried: if a class defines
``__getitem__()``, ``x in y`` is true if and only if there is a non-
negative integer index *i* such that ``x == y[i]``, and all lower
integer indices do not raise ``IndexError`` exception. (If any other
exception is raised, it is as if ``in`` raised that exception).

The operator ``not in`` is defined to have the inverse true value of
``in``.

[snipped]

-- 
Cheers.

Mark Lawrence.



More information about the Tutor mailing list