MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Howto sum up a list?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106948] Re: [mg106913] Howto sum up a list?
  • From: Tomas Garza <tgarza10 at msn.com>
  • Date: Fri, 29 Jan 2010 07:46:41 -0500 (EST)
  • References: <201001280743.CAA23521@smc.vnet.net>

I'm sure you=B4ll get better suggestions, but this one works:

In[1]:= myList={{"eggs",4},{"milk",1},{"eggs",1},{"milk",=
1}};

In[2]:= sec={#[[1]],#[[2]]}&/@Transpose[#]&/@GatherBy[myList,First]

Out[2]= {{{eggs,eggs},{4,1}},{{milk,milk},{1,1}}}

In[3]:= Flatten@{Union[#[[1]]],Total@#[[2]]}&/@sec

Out[3]= {{eggs,5},{milk,2}}

Tomas


> Date: Thu, 28 Jan 2010 02:43:17 -0500
> From: news at jonasstein.de
> Subject: [mg106913]  Howto sum up a list?
> To: mathgroup at smc.vnet.net
>
> How can i sum up the list:
>
> MyList = {{eggs, 4}, {milk, 1}, {eggs, 1}, {milk, 1}}
>
> I want to get something like:
>
> {{eggs, 5},{milk, 2}}
>
> I tyied Tally[], but that ignored the factor 4 in {eggs, 4}
>
> Thank you.
>
> --
> Jonas Stein <news at jonasstein.de>
>


  • Prev by Date: Re: Howto sum up a list?
  • Next by Date: Prime Segment Diagram; why the codes run very slowly?
  • Previous by thread: Re: Howto sum up a list?
  • Next by thread: A question about location of points on a circle