regular expression problem

Kent Johnson kent37 at tds.net
Tue May 31 08:44:23 EDT 2005


borges2003xx at yahoo.it wrote:
> hi everyone
> there is a way, using re, to test (for es) in
> a=[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14] if a list b is
> composed by three "sublists" of a separated or not by elements.
> 
> if b=[a2,a3,a4,a7,a8,a12,a13] gives true because in a
> we have [....,a2,a3,a3,...,a7,a8,...,a12,a13,...]
> or b=[a1,a2,a5,a14] gives true because in a we have
> [a1,a2,....,a5,...,a14] and so on...

difflib.SequenceMatcher can do this for you:

  >>> a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
  >>> b = [2,3,4,7,8,12,13]
  >>> import difflib
  >>> sm = difflib.SequenceMatcher(None, a, b)
  >>> sm.get_matching_blocks()
[(1, 0, 3), (6, 3, 2), (11, 5, 2), (14, 7, 0)]
  >>> b = [1, 2, 5, 14]
  >>> sm = difflib.SequenceMatcher(None, a, b)
  >>> sm.get_matching_blocks()
[(0, 0, 2), (4, 2, 1), (13, 3, 1), (14, 4, 0)]

You should test for len(sm.get_matching_blocks()) == 4 (the last element is a dummy)

Kent



More information about the Python-list mailing list