Re: Iterate through a list
- To: mathgroup at smc.vnet.net
- Subject: [mg76926] Re: [mg76886] Iterate through a list
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Wed, 30 May 2007 05:19:06 -0400 (EDT)
- References: <200705290902.FAA03917@smc.vnet.net>
On May 29, 2007, at 5:02 AM, Nick Hoffman wrote:
> Hello! I'm (somewhat) new to Mathematica and I need help to iterating
> through a list and counting the occurrence of a number.
>
> My list looks like:
>
> {{1,0},{1,1},{2,x+x^2+x^4},{2,1+x+x^2+x^4},{4,x+x^2},{4,1+x+x^2},
> {4,x+x^4},{4,1+x+x^4},{4,x^2+x^4},{4,1+x^2+x^4},{4,x^3+x^4},{4,1+x^3
> +x^4},{4,x+x^2+x^3+x^4},{4,1+x+x^2+x^3+x^4},{5,x+x^3},{5,1+x+x^3},
> {5,x^2+x^3},{5,1+x^2+x^3},{5,x+x^3+x^4},{5,1+x+x^3+x^4},{5,x^2+x^3
> +x^4},{5,1+x^2+x^3+x^4},{8,x},{8,x^2},{8,x^3},{8,x^4},{8,1+x},{8,1
> +x^2},{8,1+x^3},{8,x+x^2+x^3},{8,1+x+x^2+x^3},{8,1+x^4}}
>
>
>
> The number that I would need to keep track of is the first in each
> sub-
> list.
>
> So for the first couple {{1,0},{1,1},{2,x+x^2+x^4},{2,1+x+x^2+x^4},
> {4,x
> +x^2},........
>
> I would need to keep track of 1,1,2,2,4........
I'm not quite sure I understand your intent, First/@{{1,0},...} will
get you a list of the numbers.
counts[lst_]:=Module[{items=Union[lst]},Transpose[{items,Count[lst,#]
&/@items}]]
will get you a list of the numbers and their frequencies.
Regards,
Ssezi
- References:
- Iterate through a list
- From: "Nick Hoffman" <hoffmannick@gmail.com>
- Iterate through a list