[Python] Algorithm for Compact Representation of Tuples Using Cartesian Product

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I have the following set of tuples:

    ('red', 'hot', 'big'),
    ('red', 'hot', 'small'),
    ('red', 'cold', 'big'),
    ('blue', 'hot', 'big'),
    ('blue', 'cold', 'big'),
    ('green', 'hot', 'big'),
    ('green', 'cold', 'big')


    I would like to create a compact representation of the above, where a tuple element can be a list of allowed values. The intention is that given a representation like this, one can easily generate the original set by applying the Cartesian product. The result of the transformation might look like this:

    ('red', ['hot', 'cold'], 'big'),
    ('red', 'hot', 'small'),
    (['blue', 'green'], ['hot', 'cold'], 'big')


    I want the representation to be as compact as possible. Can you suggest an algorithm, preferably a well-known one?

    Continue reading...

Compartilhe esta Página