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: [mg107067] Re: Howto sum up a list?
  • From: stejaes <sjaeschke at gmail.com>
  • Date: Tue, 2 Feb 2010 03:26:03 -0500 (EST)
  • References: <hjrf6g$mur$1@smc.vnet.net>

On Jan 28, 8:43 am, Jonas Stein <n... at jonasstein.de> 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}
>
> Thank you.
>
> --
> Jonas Stein <n... at jonasstein.de>

This solution is great and straight forward:

{#[[1, 1]], Total[#[[
      All, 2]]]} & /@
         Split[Sort[MyList], StringMatchQ[ToString[First[#1]], \
ToString[First[#2]]] &]


  • Prev by Date: Re: What does & mean?
  • Next by Date: Re: What does & mean?
  • Previous by thread: Re: Scaled tick and label FontSizes for plots
  • Next by thread: Combining InterpolatingFunctions