Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[mg7040] How to return the position of a multidimensional list ..

5 views
Skip to first unread message

w.meeussen

unread,
May 3, 1997, 3:00:00 AM5/3/97
to

At 21:31 2-05-97 -0400, Chung Cheong Ming wrote:
>I have a list of lists (multidimensional array), if for each sublist, I
>know the first 2 elements, how to I know the position where this sublist
>situates in the list?
>
>eg. OuterList = {sublist1, sublist2, ...}
>
>I want to find where a sublist situates in the OuterList if I know the
>first 2 elements of this sublist.
>
>Joseph
>

hi,

does "Position[outerlist,{a,b,__}]" help?
********************************************************************************
Example 1:

In[38]:=it=Outer[List,{a,b},{a,b},{a,b},{a,b}]

Out[38]=
{{{{{a,a,a,a},{a,a,a,b}},{{a,a,b,a},{a,a,b,b}}},{{{a,b,a,a},{a,b,a,b}},{{a,b,
b,a},{a,b,b,b}}}},{{{{b,a,a,a},{b,a,a,b}},{{b,a,b,a},{b,a,b,b}}},{{{
b,b,a,a},{b,b,a,b}},{{b,b,b,a},{b,b,b,b}}}}}

In[39]:=pos=Position[it,{a,b,__}]
Out[39]={{1,2,1,1},{1,2,1,2},{1,2,2,1},{1,2,2,2}}

In[40]:=Extract[it,pos]
Out[40]={{a,b,a,a},{a,b,a,b},{a,b,b,a},{a,b,b,b}}

********************************************************************************
Example 2:

In[46]:=it=Flatten[Outer[dummy,{a,b},{a,b},{a,b},{a,b}]] /.dummy->List
Out[46]=
{{a,a,a,a},{a,a,a,b},{a,a,b,a},{a,a,b,b},{a,b,a,a},{a,b,a,b},{a,b,b,a},{a,b,b,
b},{b,a,a,a},{b,a,a,b},{b,a,b,a},{b,a,b,b},{b,b,a,a},{b,b,a,b},{b,b,b,a},{
b,b,b,b}}

In[47]:=pos=Position[it,{a,b,__}]
Out[47]={{5},{6},{7},{8}}
In[48]:=Extract[it,pos]
Out[48]={{a,b,a,a},{a,b,a,b},{a,b,b,a},{a,b,b,b}}
********************************************************************************

Dr. Wouter L. J. MEEUSSEN
eu00...@pophost.eunet.be
w.meeuss...@vandemoortele.be

Allan Hayes

unread,
May 3, 1997, 3:00:00 AM5/3/97
to

2 May 1997
cmc...@se.cuhk.edu.hk (Chung Cheong Ming)
[mg7040] How to return the position of a multidimensional list ..

>eg. OuterList = {sublist1, sublist2, ...}
>
>I want to find where a sublist situates in the OuterList if I know
>the first 2 elements of this sublist.

Joseph:

OuterList = {{1,2,3},{2,1,3}};
Position[OuterList,{2,1,___}]

{{2}}

If the sublists contain more complex elements you may need to limit
the search:

Position[OuterList,{2,1,___},
{1}, (*level*),
Heads->False (*not inside heads*)
]

{{2}}
Allan Hayes
h...@haystack.demon.co.uk
http://haystack.demon.co.uk


0 new messages