regular expression problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • borges2003xx@yahoo.it

    regular expression problem

    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,a1 4] 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,...,a1 2,a13,...]
    or b=[a1,a2,a5,a14] gives true because in a we have
    [a1,a2,....,a5,. ..,a14] and so on...

    thank in advance.
    giorgio borghi

  • alex23

    #2
    Re: regular expression problem

    borges2003xx@ya hoo.it wrote:[color=blue]
    > 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,a1 4] if a list b is
    > composed by three "sublists" of a separated or not by elements.[/color]

    Heya,

    Is there any particular reason why you need to use re?

    If you're using Python 2.3 or greater, the sets module might be easier
    to deal with here:
    [color=blue][color=green][color=darkred]
    >>> from sets import Set
    >>> a = Set([1,2,3,4,5,6,7,8 ,9,10,11,12,13, 14])
    >>> b = Set([2,3,4,7,8,12,13])
    >>> b.issubset(a)[/color][/color][/color]
    True[color=blue][color=green][color=darkred]
    >>> b = Set([1,2,5,14])
    >>> b.issubset(a)[/color][/color][/color]
    True[color=blue][color=green][color=darkred]
    >>> b = Set([3,7,23,200])
    >>> b.issubset(a)[/color][/color][/color]
    False

    Sets are unsorted, I'm uncertain if that's a requirement for you.

    Hope this helps.
    -alex23

    Comment

    • Kent Johnson

      #3
      Re: regular expression problem

      borges2003xx@ya hoo.it wrote:[color=blue]
      > 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,a1 4] 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,...,a1 2,a13,...]
      > or b=[a1,a2,a5,a14] gives true because in a we have
      > [a1,a2,....,a5,. ..,a14] and so on...[/color]

      difflib.Sequenc eMatcher can do this for you:
      [color=blue][color=green][color=darkred]
      >>> 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.Sequenc eMatcher(None, a, b)
      >>> sm.get_matching _blocks()[/color][/color][/color]
      [(1, 0, 3), (6, 3, 2), (11, 5, 2), (14, 7, 0)][color=blue][color=green][color=darkred]
      >>> b = [1, 2, 5, 14]
      >>> sm = difflib.Sequenc eMatcher(None, a, b)
      >>> sm.get_matching _blocks()[/color][/color][/color]
      [(0, 0, 2), (4, 2, 1), (13, 3, 1), (14, 4, 0)]

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

      Kent

      Comment

      • borges2003xx@yahoo.it

        #4
        Re: regular expression problem

        thank you again:
        i used list and not set because order in my list is important.
        in fact i'd like to apply this function to strings (or ordered
        sequences of data).
        For this reason proposed to use regular expression.
        best regards.

        Comment

        Working...