Posted By

freephys on 10/04/09


Tagged


Versions (?)

Determine if a Sequence is in another sequence in Python


 / Published in: Python
 

URL: http://stackoverflow.com/questions/425604?sort=oldest#sort-top

  1. def seq_in_seq(subseq, seq):
  2. ... while subseq[0] in seq:
  3. ... index = seq.index(subseq[0])
  4. ... if subseq == seq[index:index + len(subseq)]:
  5. ... return index
  6. ... else:
  7. ... seq = seq[index + 1:]
  8. ... else:
  9. ... return -1
  10. ...
  11. >>> seq_in_seq([5,6], [4,'a',3,5,6])
  12. 3
  13. >>> seq_in_seq([5,7], [4,'a',3,5,6])
  14. -1

Report this snippet  

You need to login to post a comment.