MathGroup Archive 1997

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

Search the Archive

Re: extracting elements from a list of lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg5827] Re: [mg5811] extracting elements from a list of lists
  • From: "w.meeussen" <w.meeussen at solair4b.eunet.be>
  • Date: Thu, 23 Jan 1997 03:19:30 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

At 00:44 22-01-97 -0500, you wrote:
>Hi,
>
>Suppose I have lists within a list, like:
>
>  myList = {{a,b}, {c,d}}
>
>and I want to extract some element (let's say the d).  
>I can request:
>
>  myList[[2,2]].
>
>Now suppose what I have the index of the element as
>the list {2,2}.  Then I can do:
>
>  Fold[Part, myList, {2,2}]
>
>Is it correct to suppose that it will first return the 
>2nd sublist, and then extract the second element from it?
>How efficient is it?  This may be a crucial point
>if I have long lists, I think.  Then isn't there a
>better way of doing this?


hi Olivier,

your guess is ok :
In[3]:=
  Fold[Part, myList, {2,2}]
Out[3]=
d

as you can see by checking the steps:
In[4]:=
  FoldList[Part, myList, {2,2}]
Out[4]=
{{{a,b},{c,d}}
,{c,d}
,d}

but The Book as good documentation on myList [[2,2]]:
In[5]:=
Part[myList,2,2]
Out[5]=
d


your general case :

In[18]:=
target={1,3,2}
Out[18]=
{1,3,2}
In[19]:=
longer=
	{
	{{a,b,c},{d,e,f},{g,h,i}},{{j,k,l},{m,n,o},{p,q,r}}
        }
Out[19]=
{{{a,b,c},{d,e,f},{g,h,i}},{{j,k,l},{m,n,o},{p,q,r}}}
In[20]:=
Part[longer,Sequence@@target]
Out[20]=
h

the trick is in Sequence[]

Dr. Wouter L. J. MEEUSSEN
w.meeussen at solair4b.eunet.be



  • Prev by Date: Re: HTML conversion question
  • Next by Date: Float32 from a file
  • Previous by thread: extracting elements from a list of lists
  • Next by thread: Re: extracting elements from a list of lists