2009 109: Difference between revisions
No edit summary |
|||
Line 23: | Line 23: | ||
[[Python Image Library]] | [[Python Image Library]] | ||
== farah: search and replace text == | |||
<source lang="python"> | |||
text = open("house_arrest.html").read() | |||
# replacements = {} | |||
# replacements["India"] = "*****"; | |||
# replacements["Azhar"] = "$$$$$$$"; | |||
import re | |||
replacements = [] | |||
replacements.append( (re.compile(r"\bindia\b", re.I), "******" ) ); | |||
replacements.append( (re.compile(r"azhar", re.I), "$$$$" ) ); | |||
for pattern, replacement in replacements: | |||
text = pattern.sub(replacement, text) | |||
print text | |||
</source> |
Revision as of 15:31, 9 December 2008
Looked at some examples:
In-class, doing Exercise 12.3
Write a function called most_frequent that takes a string and prints the letters in decreasing order of frequency. Find text samples from several different languages and see how letter frequency varies between languages. Compare your results with the tables at http://wikipedia.org/wiki/Letter_frequencies.
Recall the Historgram function from Chapter 9, p.105:
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
farah: search and replace text
text = open("house_arrest.html").read()
# replacements = {}
# replacements["India"] = "*****";
# replacements["Azhar"] = "$$$$$$$";
import re
replacements = []
replacements.append( (re.compile(r"\bindia\b", re.I), "******" ) );
replacements.append( (re.compile(r"azhar", re.I), "$$$$" ) );
for pattern, replacement in replacements:
text = pattern.sub(replacement, text)
print text