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 5 2211
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
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
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
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']
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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:...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |