Re: position of sequence of numbers in
- To: mathgroup at smc.vnet.net
- Subject: [mg107031] Re: position of sequence of numbers in
- From: JH <jlucio at ubu.es>
- Date: Mon, 1 Feb 2010 06:10:56 -0500 (EST)
- References: <hk3vv8$etb$1@smc.vnet.net>
On 31 ene, 14:18, Bob Hanlon <hanl... at cox.net> wrote: > This is wrong. I obviously didn't test this. Instead, use > > myList = {3, 1, 2, 5, 3, 4, 6, 2, 8, 9, 3, 4} > > {3,1,2,5,3,4,6,2,8,9,3,4} > > Select[Position[myList, 3], > MemberQ[Position[myList, 4] - 1, #] &] > > {{5}, {11}} > > Bob Hanlon > > ---- Bob Hanlon <hanl... at cox.net> wrote: > > ============= > > Position[list, Sequence[3, 4]] > > Bob Hanlon > > ---- JB <jke... 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 Hello I suggest this: myList = RandomInteger[{0, 8}, 1000000]; (* A list with one million integer numbers between 0 and 6 *) mySeq = myList[[1000000 - 6 ;; 1000000 - 2]]; (* Sequence to find in the list: we take a portion of the list, for example, of length 5 almost at the end *) myList2 = Table[myList[[i ;; i + Length[mySeq] - 1]], {i, Length[myList] - Length[mySeq] + 1}]; (* we generate a list with all portions of myList with the size of mySeq *) Position[myList2, mySeq] (* Here you have the positions you were searching *) JH