[Numpy-discussion] sum up to a specific value
Vincent Davis
vincent@vincentdavis....
Thu Jul 1 08:52:23 CDT 2010
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
>
More information about the NumPy-Discussion
mailing list