Skip to content
Advertisement

Find all 3^4 permutation using lists

Let’s say I have 4 distinguishable balls, and I want to distribute them into 3 cells (that’s 3^4 different ways of doing that) – how would you suggest me to demonstrate all those 81 different distributions using Python?

I want my program to return a list that contains 81 lists, each contain 3 lists representing the 3 cells, where in each sub-list there are numbers 1-4.

hope you understood what i meant.. it’s a bit complicated interpretation.

I’ll try to give an example of how my list should look like:

JavaScript

Advertisement

Answer

There is simple and bruteforce, but working, example:

JavaScript

Itertools.combinations (https://docs.python.org/3/library/itertools.html#itertools.combinations) allow you to get possible contents of first cell. From remaining balls you get possible contents of second cells, and so on.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement