Tino Dai wrote:
> Hi Everybody,
> 
>        Thank you so much for the information on sets. I think that that 
> has certain uses, but in my case I can't find a way. I have been 
> thinking about sequences in a list. Let me give you an example:
> 
> tset = [ 1,2,4,0,0,1,2,4,4]
> 
> What I want to do is detect the 1,2,4 sequence and perhaps how many.

There is nothing built-in for this. Here is a solution that uses index() 
to quickly find potential starting points. It is based on this post to 
c.l.py and the followup:
http://groups.google.com/group/comp.lang.python/msg/a03abee619ec54ef?hl=en&;

This is likely to be much faster than Lutz' solution tset is long, 
though you would have to test to be sure.

def subPositions(alist, innerlist):
     if innerlist == []:
             return
     first, start = innerlist[0], 0
     while 1:
         try:
                 p = alist.index(first, start)
         except ValueError:
                 return
         if alist[p: p + len(innerlist)] == innerlist:
                 yield p
         start = p+1

Kent
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to