Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

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: [mg106961] Re: Howto sum up a list?
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Fri, 29 Jan 2010 07:49:01 -0500 (EST)

On 1/28/10 at 2:43 AM, news at jonasstein.de (Jonas Stein) wrote:

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

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

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

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



  • Prev by Date: Re: Howto sum up a list?
  • Next by Date: Re: Howto sum up a list?
  • Previous by thread: Re: Howto sum up a list?
  • Next by thread: Re: Howto sum up a list?