-
Define a function named word_frequency.
This function has one parameter, a file object.
This function should read in a file with one word on each line,
like the lines below.
zebra
baseball
zebra
The function should return a dictionary where the keys are the words
and the values are the number of times the words appear in the file.
You should use the following algorithm
create an empty dictionary
for each line in the file
strip the linefeed from the line
if the line is already in the dictionary:
get the count for that word
increment the count by 1
assign this new value to the word
else
make an entry for the word with the value 1
return the dictionary
def word_frequency(file):
words = {}
for word in file:
word = word.strip()
if word in words:
words[word] += 1
else:
words[word] = 1
return words
-
Define the function temps_set_create
which takes a file object as its only parameter.
The function should read in a file with the minimum and maximum temperatures
for each date.
The file will have entries like this
1/1/2021 38 41
1/2/2021 39 45
1/3/2021 40 55
...
The function should create and return a set of all the MINIMUM
temperatures read from this file.
The minimum temperature is the first temperature on each line.
def temps_set_create(file):
temps = set()
for line in file:
fields = line.split()
temps.add(fields[1])
return temps
-
Define a function named dir_count.
This function has one parameter, a path to a directory.
The function should go to the directory specified by the path.
It should then loop through all the entries in the directory
and count the number of directories it finds.
You will need to use the function os.path.isdir().
The function should return the number of directories it finds.
def dir_count(path):
os.chdir(path)
total_dirs = 0
entries = os.listdir()
for entry in entries:
if os.path.isdir(entry):
total_dirs += 1
return total_dirs
-
Write the regular expression which will match each line
in the scores file below.
It should also extract the values for the full name and the score.
Bill Smith 100
Jane Doe 85
John Parker 9
I am looking for a SINGLE regular expression that matches all lines
(\w+\s\w+)\s+(\d+)