def cross(*sequences):
<EOL>wheels = map(iter, sequences)<EOL>digits = [it.next( ) for it in wheels]<EOL>while True:<EOL><INDENT>yield tuple(digits)<EOL>for i in range(len(digits)-<NUM_LIT:1>, -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>try:<EOL><INDENT>digits[i] = wheels[i].next( )<EOL>break<EOL><DEDENT>except StopIteration:<EOL><INDENT>wheels[i] = iter(sequences[i])<EOL>digits[i] = wheels[i].next( )<EOL><DEDENT><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>
From: http://book.opensourceproject.org.cn/lamp/python/pythoncook2/opensource/0596007973/pythoncook2-chp-19-sect-9.html