python -regular expression - list element

A.T.Hofkamp hat at
Wed Jun 25 13:08:30 CEST 2008

On 2008-06-25, antar2 <desothier at> wrote:
> 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

> 	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.


More information about the Python-list mailing list