Python - How to sum all combinations of a list of numbers in order to reach a goal. Use of number is optional
<p>I have a list of numbers</p>

<pre><code>lis = [497.96, 10, 5084, 156.43, 381.3, 3298.85, 625.68]
</code></pre>

<p>that I would like to sum in various ways in order to try and reach the goal number of 8276. I also need to see whether throwing away the cents or rounding helps reaching the goal. Note that use of a number is optional.</p>

<p>I tried </p>

<pre><code>from itertools import combinations
lis = [497.96, 10, 5084, 156.43, 381.3, 3298.85, 625.68]
for i in xrange(1, len(lis) + 1):   #xrange will return the values 1,2,3,4 in this loop
    if sum(list(combinations(lis, i))) == 8276:
       print list(combinations(lis, i))
</code></pre>

<p>but this gives me </p>

<pre><code>TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
</code></pre>

<p>which I'm not sure why or how to fix.</p>

<p>Since you mention:</p>

<blockquote>
  <p>I also need to see whether throwing away the cents or rounding helps reaching the goal. </p>
</blockquote>

<p>..the code below will show the closest n- combinations, and the absolute difference they show to the targeted number. </p>

<p>Works on both <code>python3</code>and <code>python2</code></p>

<pre><code>from itertools import combinations
# set the number of closest combinations to show, the targeted number and the list
show = 5
target = 8276
lis = [497.96, 10, 5084, 156.43, 381.3, 3298.85, 625.68]

diffs = []
for n in range(1, len(lis)+1):
    numbers = combinations(lis, n)
    # list the combinations and their absolute difference to target
    for combi in numbers:
        diffs.append([combi, abs(target - sum(combi))])

diffs.sort(key=lambda x: x[1])

for item in diffs[:show]:
    print(item[0], round(item[1],10))
</code></pre>

<p>The output will show the top- n closest combinations (combination / absolute difference to the targeted number):</p>

<pre class="lang-none prettyprint-override"><code>(5084, 3298.85) 106.85
(10, 5084, 3298.85) 116.85
(5084, 156.43, 3298.85) 263.28
(10, 5084, 156.43, 3298.85) 273.28
(5084, 381.3, 3298.85) 488.15
</code></pre>

<p>This shows that the closest you can get is <code>(5084, 3298.85)</code>, showing a difference of <code>106.85</code>.</p>

<hr>

<h3>Note</h3>

<p>See btw <a href="https://docs.python.org/2/tutorial/floatingpoint.html" rel="nofollow">Note on floating point calculation</a>:</p>

<hr>

<h2>Edit</h2>

<p>For the sport of it, a condensed version of the above script:</p>

<pre class="lang-py prettyprint-override"><code>from itertools import combinations 
# set the number of closest combinations to show, the targeted number and the list
show = 5
target = 8276
lis = [497.96, 10, 5084, 156.43, 381.3, 3298.85, 625.68]

diffs = [item for sublist in [[
    [combi, abs(target - sum(combi))] for combi in combinations(lis, n)
     ] for n in range(1, len(lis)+1)] for item in sublist]

diffs.sort(key=lambda x: x[1])
[print(item[0], round(item[1],10)) for item in diffs[:show]]
</code></pre>

python sum combinations permutation