# [Numpy-discussion] sum up to a specific value

Vincent Davis vincent@vincentdavis....
Thu Jul 1 09:53:17 CDT 2010

```On Thu, Jul 1, 2010 at 8:46 AM, Renato Fabbri <renato.fabbri@gmail.com> wrote:
> just a solution (not all of them)
>
> and the application happen to come up with something like 10k values
> in the array. don care waiting, but...

then something like ( I am not testing this so you might need to "fix"
it and it is ugly)
solution = None
for r in range(len(alist):
for comb in itertools.combinations(alist, r):
if sum(comb)==TheValue:
solution=comb
break
break

>
>
>
> 2010/7/1 Vincent Davis <vincent@vincentdavis.net>:
>> On Thu, Jul 1, 2010 at 3:17 AM, Renato Fabbri <renato.fabbri@gmail.com> wrote:
>>> hi,
>>> i need to find which elements of an array sums up to an specific value
>>>
>>> any idea of how to do this?
>>
>> Not sure if there is a better way but a brut force way would be to
>>
>>>>> a
>> array([[ 7.,  5.,  9.,  3.],
>>       [ 7.,  2.,  7.,  8.],
>>       [ 6.,  8.,  3.,  2.]])
>>>>> alist= a.flatten()
>>>>> alist
>> [7.0, 5.0, 9.0, 3.0, 7.0, 2.0, 7.0, 8.0, 6.0, 8.0, 3.0, 2.0]
>> asolution = []
>> for r in range(len(alist):
>>     for comb in itertools.combinations(alist, r):
>>         if sum(comb)==TheValue:
>>            asolution.append(comb)
>>
>> Now just find the comb values in the array.
>>
>> Like I said kinda brute force. Also depends if you want all solutions
>> or a solution.
>>
>> Vincent
>>
>>
>>
>>
>>
>>>
>>> best,
>>> rf
>>>
>>>
>>> --
>>> GNU/Linux User #479299
>>> skype: fabbri.renato
>>> _______________________________________________
>>> NumPy-Discussion mailing list
>>> NumPy-Discussion@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>
>
>
> --
> GNU/Linux User #479299
> skype: fabbri.renato
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
```