python -regular expression - list element

cokofreedom at gmail.com cokofreedom at gmail.com
Wed Jun 25 12:28:50 CEST 2008


On Jun 25, 11:55 am, antar2 <desoth... at yahoo.com> wrote:
> 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



More information about the Python-list mailing list