python -regular expression - list element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • antar2

    python -regular expression - list element

    Hello,

    I am a beginner in Python and am not able to use a list element for
    regular expression, substitutions.

    list1 = [ 'a', 'o' ]
    list2 = ['star', 'day', 'work', 'hello']

    Suppose that I want to substitute the vowels from list2 that are in
    list1, into for example 'u'.
    In my substitution, I should use the elements in list1 as a variable.
    I thought about:

    for x in list1:
    re.compile(x)
    for y in list2:
    re.compile(y)
    if x in y:
    z = re.sub(x, 'u', y)
    but this does not work

  • cokofreedom@gmail.com

    #2
    Re: python -regular expression - list element

    On Jun 25, 11:55 am, antar2 <desoth...@yaho o.comwrote:
    Hello,
    >
    I am a beginner in Python and am not able to use a list element for
    regular expression, substitutions.
    >
    list1 = [ 'a', 'o' ]
    list2 = ['star', 'day', 'work', 'hello']
    >
    Suppose that I want to substitute the vowels from list2 that are in
    list1, into for example 'u'.
    In my substitution, I should use the elements in list1 as a variable.
    I thought about:
    >
    for x in list1:
    re.compile(x)
    for y in list2:
    re.compile(y)
    if x in y:
    z = re.sub(x, 'u', y)
    but this does not work
    I think you misunderstand the point of re.compile, it is for compiling
    a regular expression.
    >>import re
    >>list1 = [ 'a', 'o' ]
    >>list2 = ['star', 'day', 'work', 'hello']
    >>for x in list1:
    for y in list2:
    if x in y:
    print re.sub(x, 'u', y)
    stur
    duy
    wurk
    hellu

    Comment

    • Ben Finney

      #3
      Re: python -regular expression - list element

      antar2 <desothier@yaho o.comwrites:
      for x in list1:
      re.compile(x)
      for y in list2:
      re.compile(y)
      if x in y:
      z = re.sub(x, 'u', y)
      but this does not work
      You need to frotz the hymangirator with spangule.

      That, or show us the actual result you're seeing and how it differs
      from what you expect to happen.

      --
      \ "I must say that I find television very educational. The minute |
      `\ somebody turns it on, I go to the library and read a book." -- |
      _o__) Groucho Marx |
      Ben Finney

      Comment

      • A.T.Hofkamp

        #4
        Re: python -regular expression - list element

        On 2008-06-25, antar2 <desothier@yaho o.comwrote:
        I am a beginner in Python and am not able to use a list element for
        regular expression, substitutions.
        >
        list1 = [ 'a', 'o' ]
        list2 = ['star', 'day', 'work', 'hello']
        >
        Suppose that I want to substitute the vowels from list2 that are in
        list1, into for example 'u'.
        In my substitution, I should use the elements in list1 as a variable.
        I read this as: for each string in list1, search (and replace with 'u') the
        matching substrings of each string in list2.

        Since list1 contains only strings instead of regular expressions, you could use
        string search and replace here. This makes matters much simpler.
        I thought about:
        >
        for x in list1:
        re.compile(x)
        re.compile() returns a compiled version of the RE x. Above you don't save that
        value. Ie you do something similar to

        1 + 2 * 3

        where the value 7 is computed but not saved (and thus immediately discarded
        after computing by the Python interpreter).

        Use something like

        compiled_x = re.compile(x)

        instead. 'compiled_x' is assigned the computed compiled version of string x
        now.
        for y in list2:
        re.compile(y)
        The RE module finds matches in strings, not in compiled RE expressions.
        Since you want to search through y, it has to be a string.
        if x in y:
        Here you test whether the letter in x occurs in y (both x and y are not changed
        by the re.compile() call, since that function does not alter its arguments, and
        instead produces a new result that you do not save).
        Maybe you thought you were checking whether a RE pattern match would occur. If
        so, it is not useful. Testing for a match takes about the same amount of time
        as doing the replacement.
        z = re.sub(x, 'u', y)
        but this does not work
        Instead of "re.sub(x, 'u', y)" you should use "compiled_x.sub ('u', y)" since the
        former repeats the computation you already did with the re.compile(x).

        Otherwise, the code does work, and the new string (with replacements) is saved
        in "z".

        However, since you don't save that new value, it gets lost (overwritten). You
        should save "z" in the original list, or (recommended) create a new list with
        replaced values, and replace list2 after the loop.


        Sincerely,
        Albert

        Comment

        • Chris

          #5
          Re: python -regular expression - list element

          On Jun 25, 12:32 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
          wrote:
          antar2 <desoth...@yaho o.comwrites:
          for x in list1:
             re.compile(x)
             for y in list2:
                     re.compile(y)
                     if x in y:
                             z = re.sub(x, 'u', y)
          but this does not work
          >
          You need to frotz the hymangirator with spangule.
          >
          That, or show us the actual result you're seeing and how it differs
          from what you expect to happen.
          >
          --
           \     "I must say that I find television very educational. The minute |
            `\   somebody turns it on, I go to the library and read a book."  -- |
          _o__)                                                    Groucho Marx |
          Ben Finney
          That made me laugh :D

          Why not a list comprehension ?

          ::: list1 = ['a','o']
          ::: list2 = ['star', 'day', 'work', 'hello']
          ::: [l2.replace(l1,' u') for l2 in list2 for l1 in list1 if l1 in l2]
          ['stur', 'duy', 'wurk', 'hellu']

          Comment

          • Matimus

            #6
            Re: python -regular expression - list element

            On Jun 25, 2:55 am, antar2 <desoth...@yaho o.comwrote:
            Hello,
            >
            I am a beginner in Python and am not able to use a list element for
            regular expression, substitutions.
            >
            list1 = [ 'a', 'o' ]
            list2 = ['star',  'day', 'work', 'hello']
            >
            Suppose that I want to substitute the vowels from list2 that are in
            list1, into for example 'u'.
            In my substitution, I should use the elements in list1 as a variable.
            I thought about:
            >
            for x in list1:
               re.compile(x)
                    for y in list2:
                       re.compile(y)
                            if x in y:
                                    z = re.sub(x, 'u', y)
            but this does not work
            Others have given you several reasons why that doesn't work. Nothing I
            have seen will work for words which contain both 'a' and 'o' however.
            The most obvious way to do that is probably to use a re:
            >>words = ['star', 'day', 'work', 'hello', 'halo']
            >>vowels = [ 'a', 'o' ]
            >>import re
            >>vp = re.compile('|'. join(vowels))
            >>[vp.sub('u', w) for w in words]
            ['stur', 'duy', 'wurk', 'hellu', 'hulu']
            >>>
            However, the fastest way is probably to use maketrans and translate:
            >>from string import maketrans, translate
            >>trans = maketrans(''.jo in(vowels), 'u'*len(vowels) )
            >>[translate(w, trans) for w in words]
            ['stur', 'duy', 'wurk', 'hellu', 'hulu']

            Matt

            Comment

            Working...