[Python] Cartesian product in Gray code order with itertools?

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    Is there something like Python's itertools.product() that provides the iteration through the Cartesian product of a set of sets in Gray code order? For example, supposing that such a hypothetical generator existed, and it was called gray_code_product(), then gray_code_product(['a','b','c'], [0,1], ['x','y']) would generate, in the order :

    ('a',0,'x')
    ('a',0,'y')
    ('a',1,'y')
    ('a',1,'x')
    ('b',1,'x')
    ('b',1,'y')
    ('b',0,'y')
    ('b',0,'x')
    ('c',0,'x')
    ('c',0,'y')
    ('c',1,'y')
    ('c',1,'x')

    Continue reading...

Compartilhe esta Página