On Wed, 10 Dec 2014 17:53:05 +1100, Chris Angelico wrote: > On Wed, Dec 10, 2014 at 5:44 PM, Steven D'Aprano <st...@pearwood.info> > wrote: >> It would be nice if product iterators behaved like xrange() objects and >> could perform "in" tests without exhausting the iterator, but they >> don't. That's sad. > > It'd be very difficult to do that in the general sense. But it should be > possible to have a multi-dimensional range object that behaves the way > Py3's range object does, including membership tests and stuff. (Might > already exist, for all I know.) That would do what the OP wants, I > think.
Oh yes. If the product object has n-ordinates, then you could implement it something vaguely like this: def contains(product_obj, item): if not isinstance(item, tuple): return False if len(item) != number_of_ordinates: return False for i, x in enumerate(item): if x not in ordinate[i]: return False return True but note that this too might exhaust the product iterator, if it is constructed from iterators! But for the case whether it is constructed from xrange objects, lists, tuples, etc. it would work fine. Strings are another funny case: consider what happens here: ("bc", "x", "1") in product("abcd", "wxyz", "1234") It should return False, but since string containment tests operate on substrings not just single characters, it will wrongly return True. -- Steven -- https://mail.python.org/mailman/listinfo/python-list