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: [mg106971] Re: Howto sum up a list?
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Fri, 29 Jan 2010 07:50:50 -0500 (EST)
  • References: <hjrf6g$mur$1@smc.vnet.net>

On 2010.01.28. 8:43, 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}
>

One way is

{#[[1, 1]], Total[#[[All, 2]]]} & /@ GatherBy[list, First]


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