0

I'm trying to make a function which holds all combination elements of list. The detail is like this.

This is when list elements are [1,2,3] and n=4. example,

elements=[1,2,3]
all_data=[]

for x_1 in elements:
    tmp=[]
    tmp.append(x_1)
    all_data.append(tmp)
    for x_2 in elements:
        tmp=[]
        tmp.append(x_1)
        tmp.append(x_2)
        all_data.append(tmp)
        for x_3 in elements:
            tmp=[]
            tmp.append(x_1)
            tmp.append(x_2)
            tmp.append(x_3)
            all_data.append(tmp)
            for x_4 in elements:
                tmp=[]
                tmp.append(x_1)
                tmp.append(x_2)
                tmp.append(x_3)
                tmp.append(x_4)
                all_data.append(tmp)
all_data

Output is →

[[1],
 [1, 1],
 [1, 1, 1],
 [1, 1, 1, 1],
 [1, 1, 1, 2],
 [1, 1, 1, 3],
 [1, 1, 2],
 [1, 1, 2, 1],
 [1, 1, 2, 2],
 [1, 1, 2, 3],
 [1, 1, 3],
 [1, 1, 3, 1],
 [1, 1, 3, 2],
 [1, 1, 3, 3],
 [1, 2],
 [1, 2, 1],
 [1, 2, 1, 1],
 [1, 2, 1, 2],
 [1, 2, 1, 3],
 [1, 2, 2],
 [1, 2, 2, 1],
 [1, 2, 2, 2],
 [1, 2, 2, 3],
 [1, 2, 3],
 [1, 2, 3, 1],
 [1, 2, 3, 2],
 [1, 2, 3, 3],
 [1, 3],
 [1, 3, 1],
 [1, 3, 1, 1],
 [1, 3, 1, 2],
 [1, 3, 1, 3],
 [1, 3, 2],
 [1, 3, 2, 1],
 [1, 3, 2, 2],
 [1, 3, 2, 3],
 [1, 3, 3],
 [1, 3, 3, 1],
 [1, 3, 3, 2],
 [1, 3, 3, 3],
 [2],
 [2, 1],
 .....
 [3, 2, 3],
 [3, 2, 3, 1],
 [3, 2, 3, 2],
 [3, 2, 3, 3],
 [3, 3],
 [3, 3, 1],
 [3, 3, 1, 1],
 [3, 3, 1, 2],
 [3, 3, 1, 3],
 [3, 3, 2],
 [3, 3, 2, 1],
 [3, 3, 2, 2],
 [3, 3, 2, 3],
 [3, 3, 3],
 [3, 3, 3, 1],
 [3, 3, 3, 2],
 [3, 3, 3, 3]]

But what I wanna achieve is to make a function of this doing for n times using (probably) loop statement. So, the function's output I wanna make should be going to be like,

[[1],
[1,1],
......
[1,1,1,1,......,1], #(1 of n times)
[1,1,1,1,......,2],
......
[6],
[6,6],
......
[6,6,6,6,......,6]] #(n times)

Though I've spent almost whole week to tackle this question, not yet reached to any solution. Could anyone give me some tips or happen to know any ideas? Any clue is very welcome.

Thank you.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • This is sort of like a powerset, so maybe you could adapt the powerset recipe. See [How to get all subsets of a set? (powerset)](https://stackoverflow.com/q/1482308/4518341) – wjandrea Feb 15 '20 at 15:59

1 Answers1

1

It seems that what you want is the cartesian product of k elements in the list for k in [1, 4].
If this is the case, you could use itertools.product and compute the product for k=1,2,3,4

>>> res = list(itertools.chain.from_iterable(itertools.product([1,2,3], repeat=k) for k in range(1,5)))
>>> res
[(1,), (2,), (3,), (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3), (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3), (1, 1, 1, 1), (1, 1, 1, 2), (1, 1, 1, 3), (1, 1, 2, 1), (1, 1, 2, 2), (1, 1, 2, 3), (1, 1, 3, 1), (1, 1, 3, 2), (1, 1, 3, 3), (1, 2, 1, 1), (1, 2, 1, 2), (1, 2, 1, 3), (1, 2, 2, 1), (1, 2, 2, 2), (1, 2, 2, 3), (1, 2, 3, 1), (1, 2, 3, 2), (1, 2, 3, 3), (1, 3, 1, 1), (1, 3, 1, 2), (1, 3, 1, 3), (1, 3, 2, 1), (1, 3, 2, 2), (1, 3, 2, 3), (1, 3, 3, 1), (1, 3, 3, 2), (1, 3, 3, 3), (2, 1, 1, 1), (2, 1, 1, 2), (2, 1, 1, 3), (2, 1, 2, 1), (2, 1, 2, 2), (2, 1, 2, 3), (2, 1, 3, 1), (2, 1, 3, 2), (2, 1, 3, 3), (2, 2, 1, 1), (2, 2, 1, 2), (2, 2, 1, 3), (2, 2, 2, 1), (2, 2, 2, 2), (2, 2, 2, 3), (2, 2, 3, 1), (2, 2, 3, 2), (2, 2, 3, 3), (2, 3, 1, 1), (2, 3, 1, 2), (2, 3, 1, 3), (2, 3, 2, 1), (2, 3, 2, 2), (2, 3, 2, 3), (2, 3, 3, 1), (2, 3, 3, 2), (2, 3, 3, 3), (3, 1, 1, 1), (3, 1, 1, 2), (3, 1, 1, 3), (3, 1, 2, 1), (3, 1, 2, 2), (3, 1, 2, 3), (3, 1, 3, 1), (3, 1, 3, 2), (3, 1, 3, 3), (3, 2, 1, 1), (3, 2, 1, 2), (3, 2, 1, 3), (3, 2, 2, 1), (3, 2, 2, 2), (3, 2, 2, 3), (3, 2, 3, 1), (3, 2, 3, 2), (3, 2, 3, 3), (3, 3, 1, 1), (3, 3, 1, 2), (3, 3, 1, 3), (3, 3, 2, 1), (3, 3, 2, 2), (3, 3, 2, 3), (3, 3, 3, 1), (3, 3, 3, 2), (3, 3, 3, 3)]
>>> len(res)
120

In case you don't need all the values you can just iterate over each of them:

for k in range(1,4):
   for t in itertools.product([1,2,3], repeat=k):
      ... 
abc
  • 11,579
  • 2
  • 26
  • 51
  • Huge thanks to you! Amazingly it worked! (memory is another problem tho, should be fine!) – Ryohei Kono Feb 15 '20 at 20:55
  • By any chance, do you happen to know if it is possible to use loop functions instead of itertools? In fact, memory error occurs when I run with large n value like 1000. (I am looking for some combinations of [1,2,3] only when the sum of each value in each list is equal to n) – Ryohei Kono Feb 15 '20 at 23:00
  • note that according to your goal, there might be more efficient ways to deal with your problem better than bruteforce. – abc Feb 15 '20 at 23:10