MathGroup Archive 2007

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

Search the Archive

Re: HELP: How to do count to the SUB-LIST


On 12 Okt., 08:57, zhen <wangzhen0... at gmail.com> wrote:
> Hallo, everyone
>
> I am new to Mathematica, and I get one problem now, hope to get some
> help here
>
> I got a list like this:
>
> {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 0, 0, 0, 1, 0, 0, 0},
> {0, 0, 0, 0, 1, 1, 0, 0, 2, 0}, {1, 0, 1, 0, 0, 1, 0, 1, 0, 0},
> {1, 3, 3, 4, 2, 2, 5, 3, 2, 1}, {7, 2, 2, 3, 4, 4, 0, 3, 1, 3}}
>
> I need to get a list to show the number of non zero number in each sub
> list, as to say, I need to get a list like this:
>
> {0,3,3,4,10,9}
>
> Thanks alot!

Hello

Using Map and Count and your list (which is named lis here) the
following code does what you want:

(Count[#1, Except[0]] & ) /@ lis

An alternative would be:

(Length[Select[#1, #1 != 0 & ]] & ) /@ lis

or without using pure functions:

Clear[x, f1, f2];
f1[x_] := x != 0;
f2[x_] := Length[Select[x, f1]];
Map[f2, lis]

And so on

Best Regards
Norbert Marxer








  • Prev by Date: Re: HELP: How to do count to the SUB-LIST
  • Next by Date: Re: HELP: How to do count to the SUB-LIST
  • Previous by thread: Re: HELP: How to do count to the SUB-LIST
  • Next by thread: Re: HELP: How to do count to the SUB-LIST