MathGroup Archive 1998

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

Search the Archive

Re: Request for help: working with multi-level lists


Hi,

The way that I would do this is to use Cases, as in:

realList = Cases[ li, {1, r_, _}->r ]

to get a list of the numbers, where li is your list, and then to get the
mean use

mean = (Plus@@realList)/Length[realList]

You could turn the above into a function, as in

Mean[li_List] := (Plus@@li)/Length[li]

Kmean[li_, i_Integer] := Mean[ Cases[li, {i, r_, _}->r] ]

Carl Woll
Dept of Physics
U of Washington

KCConnolly wrote:

> I have a list of 10 elements, each of which is a list of three elements
> (let's say in each case an integer, a real number, and a string). I am
> looking for the most elegant way to select those first-level elements
> (i.e., the lists) whose integer element is equal to a particular value
> (let's say "1"), and then to obtain the mean of the real number
> elements of the lists selected.  This seems as if it should be simple,
> but everything I try leads to Part specification errors.  Any help
> would be greatly appreciated.




  • Prev by Date: Re: Request for help: working with multi-level lists
  • Next by Date: Re: recursive relation problem ?
  • Previous by thread: Request for help: working with multi-level lists
  • Next by thread: Re: Request for help: working with multi-level lists