[Python] Is there an iterator in Python that gives the product but omits permutations of the...

Discussão em 'Python' iniciado por Stack, Setembro 10, 2024.

  1. Stack

    Stack Membro Participativo

    Assume that I want to assign a color to 5 different balls and have 3 colors r,b and g. I would like to iterate over all these combinations. Preferably I would want to omit combinations that are equal up to permutation of the color classes. That means for example that I want to consider (r,b,b,b,b) and (g,r,r,r,r) as equal. I could use

    itertools.product('rbg', repeat=5)


    but this would overcount by a lot. Maybe

    itertools.combinations_with_replacement('rbg', 5)


    would be better? I am not sure if this approach would not miss some. But it would certainly see (r,b,b,b,b) and (g,r,r,r,r) as different combinations. Is there anything better I could do?

    Continue reading...

Compartilhe esta Página