Beginners typically learn the usage of Python language constructs in isolation, which is good since it avoids mental overload. However, once you have learnt the basics, it is essential to see practical examples of how Python constructs are integrated to solve real-world problems. This session shows how to integrate several basic Python constructs to solve a text processing problem.
Task: Count the number of each word (ignoring case) in the Gettysburg address (in the file "getty.txt"), and print the top 10 words.
Skills:
In [1]:
# read in the file "getty.txt" into a single string variable
In [2]:
# strip text of punctuation
In [3]:
# convert to lower case
In [4]:
# split string into words
In [5]:
# wrtie a function that counts the number of each word (loop version)
In [6]:
# wrtie a function that counts the number of each word (list comprehension version)
In [ ]:
# print the top 10 words
What is the the correct value after running the following code?
s = """
This old man;
he played one.
"""
print s.count('this')
What is the final value of s after running the following code?
s = """
This old man;
he played one.
"""
s = s.strip().translate(None, string.punctuation).upper()
This old man;\nhe played one.
THIS OLD MAN\nHE PLAYED ONE
THIS OLD MANHE PLAYED ONE
this old manhe played one
THIS OLD MAN;\NHE PLAYED ONE.
Which of the following ways of removing punctuation from a string s is the slowest?
new_s = []
for char in s:
if char not in string.punctuation:
new_s.append(char)
new_s = ''.join(new_s)
new_s = ''
for char in s:
if char not in string.punctuation:
new_s += char
new_s = ''.join([char for char in s if char not in string.punctuation])
new_s = s.translate(None, string.punctuation)
filter(lambda x: x not in string.punctuation, s)
In [ ]: