-3

In Python, I am trying to add all of the different combinations of the elements in a list of N lists, where N is a variable. Specifically, I'm working with a list containing N copies of the list [1, 2, 3, 4, 5, 6].

So let's assume N = 3. I want to cycle through the first list, adding:

 6 + 6 + 6
 5 + 6 + 6
 ...
 1 + 6 + 6`

then increment the second guy once and start adding:

6 + 5 + 6
5 + 5 + 6
...
1 + 1 + 1  # ultimately to

after of course I increment the third guy. This specific example can be done with 3 nested for loops, but of course, I need the number of loops to change. I search around the web and this forum for answers and wasn't quite able to grasp what I should do. People have been saying to use recursive functions, but I've never even heard of that until I looked this up. So it's kind of hard to grasp.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
ZachR
  • 89
  • 2
  • 7
  • 1
    What have you tried, can you show us your attempts? Maybe we can help you fix them. – MooingRawr Jan 13 '17 at 21:17
  • SO is neither a code-writing nor tutorial service. Review [ask]. – jonrsharpe Jan 13 '17 at 21:17
  • Use [`itertools.product`](https://docs.python.org/3/library/itertools.html#itertools.product). `list(map(sum, product([1,2,3,4,5,6], repeat=n)))` – Patrick Haugh Jan 13 '17 at 21:18
  • The below responses helped. I haven't really coded up an attempt because I could not think of/find an idea. I looked everywhere and just couldn't find anything I could understand, including the link posted by Tadhg McDonald-Jensen. I do apologize if anyone feels I have asked a bad question or misused the site, but I did not know where to find the answer to my question and thought I would just ask it myself. Thanks for the assistance everyone. – ZachR Jan 17 '17 at 13:23

2 Answers2

1

In order to achieve this, you may use itertools.product as:

from itertools import product
my_list = [1, 2, 3, 4, 5, 6]
N=3 

#                         value of `N` v
for s in product(my_list[::-1], repeat=N):
    print '{} : {}'.format(s[::-1], sum(s))
    #           No need to reverse here ^
    #           since sum(s[::-1]) and sum(s) will give same result 

which will print:

# (combination) : sum
(6, 6, 6) : 18
(5, 6, 6) : 17
(4, 6, 6) : 16
(3, 6, 6) : 15
(2, 6, 6) : 14
(1, 6, 6) : 13
...
(6, 1, 1) : 8
(5, 1, 1) : 7
(4, 1, 1) : 6
(3, 1, 1) : 5
(2, 1, 1) : 4
(1, 1, 1) : 3

If you just want to store the value in list, you may use list comprehension expression as:

>>> [sum(s) for s in product(my_list[::-1], repeat=3)]
[18, 17, 16, 15, 14, 13, 17, 16, 15, 14, 13, 12, 16, 15, 14, 13, 12, 11, 15, 14, 13, 12, 11, 10, 14, 13, 12, 11, 10, 9, 13, 12, 11, 10, 9, 8, 17, 16, 15, 14, 13, 12, 16, 15, 14, 13, 12, 11, 15, 14, 13, 12, 11, 10, 14, 13, 12, 11, 10, 9, 13, 12, 11, 10, 9, 8, 12, 11, 10, 9, 8, 7, 16, 15, 14, 13, 12, 11, 15, 14, 13, 12, 11, 10, 14, 13, 12, 11, 10, 9, 13, 12, 11, 10, 9, 8, 12, 11, 10, 9, 8, 7, 11, 10, 9, 8, 7, 6, 15, 14, 13, 12, 11, 10, 14, 13, 12, 11, 10, 9, 13, 12, 11, 10, 9, 8, 12, 11, 10, 9, 8, 7, 11, 10, 9, 8, 7, 6, 10, 9, 8, 7, 6, 5, 14, 13, 12, 11, 10, 9, 13, 12, 11, 10, 9, 8, 12, 11, 10, 9, 8, 7, 11, 10, 9, 8, 7, 6, 10, 9, 8, 7, 6, 5, 9, 8, 7, 6, 5, 4, 13, 12, 11, 10, 9, 8, 12, 11, 10, 9, 8, 7, 11, 10, 9, 8, 7, 6, 10, 9, 8, 7, 6, 5, 9, 8, 7, 6, 5, 4, 8, 7, 6, 5, 4, 3]
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

You can use itertools.combinations_with_replacement to solve this problem.

In []: from itertools import combinations_with_replacement
In []: for combination in combinations_with_replacement(range(1, 7), 3):
           print("Combination: ", combination, " Sum: ", sum(combination))
   ....:     
Combination: (1, 1, 1)  Sum: 3
Combination: (1, 1, 2)  Sum: 4
Combination: (1, 1, 3)  Sum: 5
Combination: (1, 1, 4)  Sum: 6
Combination: (1, 1, 5)  Sum: 7
Combination: (1, 1, 6)  Sum: 8
.
.
.
Combination: (4, 5, 6)  Sum: 15
Combination: (4, 6, 6)  Sum: 16
Combination: (5, 5, 5)  Sum: 15
Combination: (5, 5, 6)  Sum: 16
Combination: (5, 6, 6)  Sum: 17
Combination: (6, 6, 6)  Sum: 18

If you want to store the sums in a list, you can use a list comprehension as:

list_of_sums = [sum(combination) for combination in combinations_with_replacement(range(1, 7), 3)]
Kshitij Saraogi
  • 6,821
  • 8
  • 41
  • 71