[Python] Get all possible ways to put variable list of items into N bins [closed]

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

  1. Stack

    Stack Membro Participativo

    I have a list of items which can be any length (but will never contain duplicates of the same item), and a set of N bins which can each hold zero or one item. What's the easiest way to get all possible ways to put the items into the bins? I'm pretty sure I have a recursive solution that would work, but I want to know if there's a simpler option.

    Ideally I only need permutations that fill as many bins as possible.

    Examples for N = 3:

    [A,B] -> [[A,B,_],[A,_,B],[B,A,_],[_,A,B],[B,_,A],[_,B,A]]

    [A,B,C,D] -> [[A,B,C],[A,C,B],[B,A,C],[B,C,A],[C,A,B],[C,B,A],
    [A,B,D],[A,D,B],[B,A,D],[B,D,A],[D,A,B],[D,B,A],
    [A,D,C],[A,C,D],[D,A,C],[D,C,A],[C,A,D],[C,D,A],
    [D,B,C],[D,C,B],[B,D,C],[B,C,D],[C,D,B],[C,B,D]]

    Continue reading...

Compartilhe esta Página