By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,571 Members | 991 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,571 IT Pros & Developers. It's quick & easy.

python -regular expression - list element

P: n/a
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

Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies

P: n/a
On Jun 25, 11:55 am, antar2 <desoth...@yahoo.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
Jun 27 '08 #2

P: n/a
antar2 <de*******@yahoo.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
Jun 27 '08 #3

P: n/a
On 2008-06-25, antar2 <de*******@yahoo.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
Jun 27 '08 #4

P: n/a
On Jun 25, 12:32*pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
antar2 <desoth...@yahoo.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']
Jun 27 '08 #5

P: n/a
On Jun 25, 2:55*am, antar2 <desoth...@yahoo.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(''.join(vowels), 'u'*len(vowels))
[translate(w, trans) for w in words]
['stur', 'duy', 'wurk', 'hellu', 'hulu']

Matt
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.