Get all possible combinations of a list including repeating element at a given lenght

So I’ve been trying this for a while now, but I can’t seem to get a hold of it.
It seems like such a simple task:
From my input(list) I want to create all possible lists with the given length. So for instance:
Input list: [a,b]
Input length: 3

Output: [[a,a,a],[a,a,b],[a,b,a],[a,b,b],[b,a,a],[b,a,b],[b,b,b],[b,b,a]] (8 results because 2x2x2=8)
My knowledge with python is not that good, I can read it but find it hard to produce code on my own.
Basically I need some node or code that gives all the possibilities of a list like: 2^2 and not like 2! like the combination node does.

It’s like a safe-deposit: combination at a given length and the digits can be used multiple times.
A safe with 4 positions ranging from 0-9 can have 10x10x10x10=10.000 possibilities.
List.Permutation does give me lists at a given length, but isn’t giving lists that include the options of having to use an item more than once.

In case of my script both the amount of items and length of desired lists are variable.

Hoping someone here can help me, thanks in advance.

1 Like

Maybe others may know a fancy way but if I was the one who approach this problem, I would write a code that gives permutation results but only store the result in a list only if the result is not in the list. This way your result will be a combination instead of permutation.

There’s probably more programmatically efficient methods, but maybe like this?

Fun little function… will add it to a future build of Crumple.

2 Likes

Thats exactly what I meant, I tried something with itertools too but didn’t results. Thanks again Gavin!

1 Like

Hello,
itertools.product can also be used

[image]

3 Likes

Thanks both for the solutions, both work very well!
However, the itertool.product version is significantly fast to run. (according to the Tuneup addin)

So @GavinCrump, this could be an even more optimized addition to the Crumple package :wink:

Again, I really appreciate the effort and fast responses.

Yes that does indeed appear to be optimized! Will implement instead, thanks @c.poupin for your Python knowledge as always.

2 Likes

Out of curiosity, what is the intended use here? I am wondering if there is a more effective way to produce the goal.

Note that there is a significant amount of data produced, at scale the larger data sets will be crippling for use due to the lack of memory’s capacity to serialize the world (ie: take a list that is 11 items long and has 11 options, and you have 285,311,670,611 options which will be returned).

At the time of asking I was creating a script that allows the user to review all possibilities of a mass-study. I work architecture firm and in the early stages of the design, architects produces various masses the need to be combined in a row, as in terraced housing.
When all the options are created, the architect can go through all the options to see which of the variants he likes the most. We don’t creating rows long than 4 or 5 houses at most, but yes, this type of usage can create large amounts of data.

I finished the script and had dynamo place the combinations of masses on rows of 25 combinations, so it creates a big field of options.
Furthermore, the script also gather information from the placed combinations, such as volume, surface-area, etc. floorarea.

Running time is surprisingly faster than I thought, about 30 seconds with a row of 3 and 6 possible masses (creating 666=216 different combinations).

Next step is to use this in generative design, have it favour different variables so the architects can also measure into efficiency.

1 Like

Pulling one permutation are a time is likely better in that case (and kind of where I thought you were going with it).

The package Generative Design at Hogwarts has a node called nth permutation which has some python which covers how to do this where a list can only be used once - in your case I would create that effect by using a List.OfRepeatedItems. The advantage there is it will allow the NSGA2 algorithm in Generative Design to explore the pattern - you are however limited to a depth of about 17 or 18 items on your list to make this work effectively (a function on the upper limit of the number sliders - which pop into scientific notation around 18! or 19!).

1 Like