473,804 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2232
On Jun 25, 11:55 am, antar2 <desoth...@yaho o.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*******@yaho o.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*******@yaho o.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+hate s-s...@benfinney. id.au>
wrote:
antar2 <desoth...@yaho o.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...@yaho o.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(''.jo in(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
3853
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 less - during my own installation of Python 2.3 on Fedora Core 1 Linux for a friend of mine). Anyway, HTH, L.
10
3091
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
2060
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 during the final linking stage. Several PyThread_* symbols are undefined (for details see the output below). In order to get DCOracle2 support working I've also set the LDFLAGS environment variable to "-lpthread -lcl" (as mentioned in the...
2
14363
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 because of a binary module which has to be compiled with Visual C 6. My scripts start with a '#!/usr/bin/env python' shebang, as God intended. Now, I assume I can make cmd.exe run foo.py by asociating *.py with the python interpreter.
1
2954
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 jfj@cluster:~/> valgrind python ==10517== Memcheck, a.k.a. Valgrind, a memory error detector for x86-linux.
3
2613
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 problem cann't be reproduced easily in my system and the call stack generated by the application is given below. Please note that the call stack generation is taken from crash dump file.
0
1582
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 ( -1) / 197 closed ( +1) / 404 total ( +0) New / Reopened Patches ______________________
0
247
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 ( +2) / 234 closed ( +0) / 462 total ( +2) New / Reopened Patches ______________________
11
4924
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: ============================ #include <iostream> #include <stdafx.h>
0
1812
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 creating build/temp.solaris-2.10-i86pc-2.5/home/ecuser/Python-2.5.1/ Modules/_ctypes/libffi creating build/temp.solaris-2.10-i86pc-2.5/home/ecuser/Python-2.5.1/ Modules/_ctypes/libffi/src
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10351
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10096
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9174
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7638
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6866
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4311
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 we have to send another system
3
3002
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.