Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #12105
| From | Daniel <dosadchy@its.jnj.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Sum elements in a list with conditions |
| Date | 2012-10-19 06:46 +0000 |
| Message-ID | <k5qt0r$nbf$1@smc.vnet.net> (permalink) |
| Organization | Time-Warner Telecom |
> I have a list of sublist of pairs. I wish sum the
> second elements of
> the sublists when the first elements are equals
> Example:
> Int[]:={{1, a}, {2, b}, {1, c}, {2, d}, {2, e}, {3,
> f}}
> I hope
> Out[]:= {{1, a + c}, {2, b + c + e}, {3, f}}
>
> I can do it as is it is shown below but could any
> body find and
> easier solution.
>
>
> list1 = {{1, a}, {2, b}, {1, c}, {2, d}, {2, e}, {3,
> f}};
> list2 = GatherBy[list1, First];
> {l1, l2} = Transpose[Plus @@@ GatherBy[list, First]];
> Transpose[{l1/Length /@ list2 , l2}]
>
In[1]:= list1 = {{1, a}, {2, b}, {1, c}, {2, d}, {2, e}, {3, f}};
In[4]:= list2 = GatherBy[list1, First];
In[3]:= {#[[1, 1]], #[[All, 2]] // Total} & /@ list2
Out[3]= {{1, a + c}, {2, b + d + e}, {3, f}}
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: Sum elements in a list with conditions Daniel <dosadchy@its.jnj.com> - 2012-10-19 06:46 +0000
csiph-web