Nested loops is strangely slow, totally at a loss.
Terry Reedy
tjreedy at udel.edu
Wed Dec 10 02:16:37 EST 2014
On 12/10/2014 1:53 AM, Chris Angelico wrote:
> On Wed, Dec 10, 2014 at 5:44 PM, Steven D'Aprano <steve at 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.
Itertools are general tools for building specialized objects.
itertools.product provides the iter method.
class ReitProd(): # untested
def __init__(self, reiterable, n):
self.reit = reiterable # must support 'in'
self.n = n
def __iter__(self):
return itertools.product(self.reit, repeat=self.n)
def __contains__(self, seq):
if len(seq) != self.n:
return False
for i, item in enumerate(it):
if item not in self.reit:
return False
return True
--
Terry Jan Reedy
More information about the Python-list
mailing list