python -regular expression - list element

A

antar2

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
 
C

cokofreedom

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
 
C

Chris

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']
 
M

Matimus

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top