MathGroup Archive 1998

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

Search the Archive

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


KCConnolly skrev:
> 
> 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.

this should do the job...

first i define some data (not 10, only 3)

	In[1]:=
	data={{1,3.14,"Monkey"},{2,2.71,"Donkey"},{1,5.32,"Junkie"}}
	Out[1]=
	{{1,3.14,"Monkey"},{2,2.71,"Donkey"},{1,5.32,"Junkie"}}

then i select the ones satisfying the criteria

	In[2]:=
	tmp=Select[data,(#[[1]]==1)&]
	Out[2]=
	{{1,3.14,"Monkey"},{1,5.32,"Junkie"}}

and then calculate the mean of the real's

	In[3]:=
	mean=Plus@@Transpose[tmp][[2]]/Length[tmp]
	Out[3]=
	4.23


  • Prev by Date: Re: Request for help: working with multi-level lists
  • Next by Date: Re: Request for help: working with multi-level lists
  • Previous by thread: Re: Request for help: working with multi-level lists
  • Next by thread: unwrapping ArcTan