Out of curiousity, is "recursion" the desirable way(or is it pythonic way) ? How would one do it in the imperative way ?
Dan Bishop wrote: > > it could be done by a recursive call, but i am interested in using and > > understanding generators. > > def cross(*args): > """Iterates over the set cross product of args.""" > if not args: > return > elif len(args) == 1: > for x in args[0]: > yield (x,) > else: > for x in args[0]: > for y in cross(*args[1:]): > yield (x,) + y > > >>> cross(['a0', 'a1', 'a2'], ['b0', 'b1'], ['c0', 'c1', 'c2', 'c3']) > <generator object at 0xb7df072c> > >>> list(_) > [('a0', 'b0', 'c0'), ('a0', 'b0', 'c1'), ('a0', 'b0', 'c2'), ('a0', > 'b0', 'c3'), ('a0', 'b1', 'c0'), ('a0', 'b1', 'c1'), ('a0', 'b1', > 'c2'), ('a0', 'b1', 'c3'), ('a1', 'b0', 'c0'), ('a1', 'b0', 'c1'), > ('a1', 'b0', 'c2'), ('a1', 'b0', 'c3'), ('a1', 'b1', 'c0'), ('a1', > 'b1', 'c1'), ('a1', 'b1', 'c2'), ('a1', 'b1', 'c3'), ('a2', 'b0', > 'c0'), ('a2', 'b0', 'c1'), ('a2', 'b0', 'c2'), ('a2', 'b0', 'c3'), > ('a2', 'b1', 'c0'), ('a2', 'b1', 'c1'), ('a2', 'b1', 'c2'), ('a2', > 'b1', 'c3')] -- http://mail.python.org/mailman/listinfo/python-list