[Tutor] Detecting sequences in lists

Lutz Horn lutz.horn at fastmail.fm
Mon Aug 27 15:50:10 CEST 2007


Hi,

DIY is easy.

On Mon, 27 Aug 2007 09:20:53 -0400, "Tino Dai" <tinoloc at gmail.com> said:
> What I want to do is detect the 1,2,4 sequence and perhaps how many.

>>> tset = [ 1,2,4,0,0,1,2,4,4]
>>> s = [1, 2, 4]
>>> c = 0
>>> for i in range(len(tset) - len(s)):
...     if tset[i:i+len(s)] == s:
...             c = c + 1
...             print "found at %d" % i
... 
found at 0
found at 5
>>> print c
2

Regards
Lutz

-- 
<http://www.fastmail.fm/mail/?STKI=600622>



More information about the Tutor mailing list