Determine if a Sequence is in another sequence in Python


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  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

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.