472,354 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

python -regular expression - list element

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
5 2162
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or...
10
by: Berthold Hoellmann | last post by:
Hello, When I use ./configure --with-thread --with-fpectl --with-signal-module \ --with-pymalloc --enable-shared --with-cxx=g++ make test on 2.3.3 I get
2
by: Olaf Meyer | last post by:
I'm having some problems compiling Python 2.3.3 on HP-UX (B.11.00). I've tried sevral different options for the configure script (e.g. enabling/disabling gcc, aCC) but I always get the same problem...
2
by: Jorgen Grahn | last post by:
I couldn't think of a good solution, and it's hard to Google for... I write python command-line programs under Win2k, and I use the bash shell from Cygwin. I cannot use Cygwin's python package...
1
by: Jerald | last post by:
Running python 2.3.4 on valgrind (a tool like purify which checks the use of uninitialized memory, etc), gives a lot of errors. See below. jfj@cluster:~/> python -V Python 2.3.4...
3
by: Saravanan | last post by:
Hello, Im running Python Application as a Windows Service (using windows extensions). But, sporadically the application crashes (crash in Python23.dll) and this stops the service. This...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 391 open ( +7) / 3028 closed (+12) / 3419 total (+19) Bugs : 906 open ( -3) / 5519 closed (+19) / 6425 total (+16) RFE : 207 open...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 402 open ( +6) / 3360 closed ( +6) / 3762 total (+12) Bugs : 861 open ( -3) / 6114 closed (+27) / 6975 total (+24) RFE : 228 open...
11
by: Osiris | last post by:
I have these pieces of C-code (NOT C++ !!) I want to call from Python. I found Boost. I have MS Visual Studio 2005 with C++. is this the idea: I write the following C source file:...
0
by: mg | last post by:
When make gets to the _ctypes section, I am getting the following in my output: building '_ctypes' extension creating build/temp.solaris-2.10-i86pc-2.5/home/ecuser/Python-2.5.1/ Modules/_ctypes...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.