MathGroup Archive 2010

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

Search the Archive

Re: Howto sum up a list?


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?