[Tutor] Best way to alter sections of a string which match dictionary keys?

Bob Gailer bgailer at alum.rpi.edu
Fri Jan 2 13:24:17 EST 2004

At 12:55 AM 1/2/2004, SSokolow wrote:

>I have a proxy which stores a list of URLs visited withing a certain 
>server as keys in a dictionary and currently all the values are just set 
>to 1 (I have some ideas for future expansion though.) Originally this was 
>just because it was the easiest way to prevent duplication of entries.
>Example: {'/2048.html': 1, '/index.html': 1, '/1.html': 1}
>My problem is that I want to add <img src="_proxy/checkmark.png" /> beside 
>every hyperlinked block of text which links to one of these URLs. A while 
>ago when I made the Perl CGI version (aborted), the only way I could find 
>was to waste resources using this code:
>while( ($key, $value)=each %viewedEpisodes) {
>  $file=~s/(.*)<a(.*)href="($key)"(.*)>(.*)/$1<img 
> src="$imgDir\/check.png"><a$2href="$3"$4>$5/ig;
>which is roughly equivalent to: (I didn't have time to test this)
>for URL in viewedEpisodes|.keys()
>    file = re.sub(r'||(?i)||(<a.*?href="' + URL + r'".*?)', file, ||r'<img 
> src="' + imgDir + r'/check.png">\1')

I am reluctant to answer this question (since it takes some time and energy 
to construct a thoughtful reply), but since no one else has...

This question is hard to tackle for the following reasons. I hope you will 
be able to restructure the question so we can really help you, and that in 
the future you will be able to ask questions that are clear and to the point.

1) many of us do not know Perl. so including Perl code does not help us.

2) "I didn't have time to test this" is obvious since the code does not 
compile. Please either present tested code or ask questions without code. 
It is also unclear what you want from the vertical bars in the re match 
string (vertical bars are used as OR operators to separate regular expressions.

3) what would be most useful is to give us a before and after example e.g. 
'/2048.html becomes blahblahblah (I have no idea what the desired outcome is)

4) file is a built-in function. It is inadvisable to rebind the name to a 

>I could just use that code in Python (It would work, right?) but it would 
>be a complete waste of processor cycles.
>What would be the best way to accomplish the task without wasting so much 
>time looping?

5) How much time is "wasted"? Did you measure it? Processing multiple items 
requires looping. The alternatives to for statements in Python is list 
comprehension and while.

6) usually we like to write code that does the job, then assess the 
efficiency aspect. Often the code we write does the job in less time than 
we thought, and is OK as is.

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell 
-------------- next part --------------

Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.556 / Virus Database: 348 - Release Date: 12/26/2003

More information about the Tutor mailing list