Re: position of sequence of numbers in list
- To: mathgroup at smc.vnet.net
- Subject: [mg106996] Re: [mg106976] position of sequence of numbers in list
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Sun, 31 Jan 2010 05:56:21 -0500 (EST)
- References: <201001301212.HAA25132@smc.vnet.net>
Well,
Sorry for so many posts - apparently I did not include the code for
<myPosition> function mentioned in my second post, while inluded the timings
for it. Here it is:
myPosition =
Compile[{{lst,_Integer,2},{target,_Integer,1},{nresults,_Integer}},
Module[{i=1,len = Length[lst],positions =
Table[0,{If[nresults!=-1,nresults,Length[lst]]}],pctr=0,
lcnresults = If[nresults!=-1,nresults,Length[lst]]},
While[i<=len&&pctr<lcnresults,
If[lst[[i++]]==target,positions[[++pctr]]=i-1]];
Take[positions,pctr]]]
Regards,
Leonid
On Sat, Jan 30, 2010 at 4:12 AM, JB <jkerrb at gmail.com> wrote:
> Hi,
>
> 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}]
>
> Are there other ways to do this?
> What is the best way when dealing with large lists?
>
> Thanks,
> JB
>
>
- References:
- position of sequence of numbers in list
- From: JB <jkerrb@gmail.com>
- position of sequence of numbers in list