[Numpy-discussion] sum up to a specific value

Renato Fabbri renato.fabbri at gmail.com
Thu Jul 1 10:46:11 EDT 2010


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...



2010/7/1 Vincent Davis <vincent at vincentdavis.net>:
> On Thu, Jul 1, 2010 at 3:17 AM, Renato Fabbri <renato.fabbri at 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 at scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
GNU/Linux User #479299
skype: fabbri.renato



More information about the NumPy-Discussion mailing list