Re: position of sequence of numbers in list
- To: mathgroup at smc.vnet.net
- Subject: [mg107008] Re: position of sequence of numbers in list
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sun, 31 Jan 2010 05:58:34 -0500 (EST)
On 1/30/10 at 7:12 AM, jkerrb at gmail.com (JB) wrote:
>What is the most efficient way to find the position of the beginning
>of a sequence of numbers from a list?
>I found a couple of ways:
>find 3,4 in list={1,2,3,4,5};
>1. pos=Intersection[Position[list,3],(Position[list,4])+1]
>2. pos=Position[Partition[list,2,1],{3,4}]
note this will fail if the things searched for are not
sequential. That is:
In[1]:= list = {1, 2, 3, 4, 5};
In[2]:= pos = Position[Partition[list, 2, 1], {1, 4}]
Out[2]= {}
>Are there other ways to do this? What is the best way when dealing
>with large lists?
Here is another way to the task
In[3]:= theList = RandomInteger[100, 5]
Out[3]= {94,40,22,10,40}
In[4]:= Pick[Range@Length@theList, theList, Alternatives @@ {10, 22}]
Out[4]= {3,4}
and for non-sequential items
In[5]:= Pick[Range@Length@theList, theList, Alternatives @@ {94, 22}]
Out[5]= {1,3}