python -regular expression - list element

python -regular expression - list element

Post by antar » Thu, 26 Jun 2008 18:55:16


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
 
 
 

python -regular expression - list element

Post by cokofreedo » Thu, 26 Jun 2008 19:28:50


I think you misunderstand the point of re.compile, it is for compiling
a regular expression.

for y in list2:
if x in y:
print re.sub(x, 'u', y)
stur
duy
wurk
hellu

 
 
 

python -regular expression - list element

Post by Ben Finne » Thu, 26 Jun 2008 19:32:26

antar2 < XXXX@XXXXX.COM > writes:


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
 
 
 

python -regular expression - list element

Post by A.T.Hofkam » Thu, 26 Jun 2008 20:08:30


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.


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.


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.


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.


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
 
 
 

python -regular expression - list element

Post by Chri » Thu, 26 Jun 2008 20:33:12

On Jun 25, 12:32m, Ben Finney <bignose+ XXXX@XXXXX.COM >

> > e.compile(x) >>>> or y in list2:> > > e.compi>e>y)
> > gt;f>x in y:
> > > >e.sub(x, 'u', y)
> > but>th>s does not work
>
> You need to frotz the hymangir>to> with spangule.
>
> That, or show us the actual result you're seeing>and how it differs
> from what yo> e>pect >o happen.
>
> --
> "I must say that I find television very educ>tional. 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']
 
 
 

python -regular expression - list element

Post by Matimu » Fri, 27 Jun 2008 01:35:00


> 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: >> e.compile(x)> > for y in li>t2:
> e.c>mpile(y)
> gt;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 th>>>is probably to use a re:

>>> words = ['star', 'd>>>, 'work', 'hello', 'halo>>> >>> vowels >>> 'a', 'o' ]
>>> import re
>>> vp =>>>.compile('|'.join(vowels))
>>> [vp.sub('u', w) for w in words]
['stur', 'd>>>, 'wurk', 'hellu', 'hulu']
>>>

However, the fastest way is probably to u>>>maketrans and translate:

>>> from strin>>>mport maketrans, translate
>>> trans = maketrans(''.j>>>(vowels), 'u'*len(vowels))
>>> [translate(w, trans) for w in words]
['stur', 'duy', 'wurk', 'hellu', 'hulu']

Matt