I am having trouble figuring out how to write a code to complete this assignment in Python:

Implement the build_dictionary() function to build a word frequency dictionary from a list of words.

Ex: If the words list is:

["hey", "hi", "Mark", "hi", "mark"]
the dictionary returned from calling build_dictionary(words) is:

{'hey': 1, 'hi': 2, 'Mark': 1, 'mark': 1}
Ex: If the words list is:

["zyBooks", "now", "zyBooks", "later", "zyBooks", "forever"]
the dictionary returned from calling build_dictionary(words) is:

{'zyBooks': 3, 'now': 1, 'later': 1, 'forever': 1}
The main code builds the word list from an input string, calls build_dictionary() to build the dictionary, and displays the dictionary sorted by key value.

Ex: If the input is:

hey hi Mark hi mark
the output is:

Mark: 1
hey: 1
hi: 2
mark: 1

This is what I have been given:

# The words parameter is a list of strings.
def build_dictionary(words):
# The frequencies dictionary will be built with your code below.
# Each key is a word string and the corresponding value is an integer
# indicating that word's frequency.

'''your code goes here'''

# The following code asks for input, splits the input into a word list,
# calls build_dictionary(), and displays the contents sorted by key.
if __name__ == '__main__':
words = input().split()
your_dictionary = build_dictionary(words)
sorted_keys = sorted(your_dictionary.keys())
for key in sorted_keys:
print(key + ': ' + str(your_dictionary[key]))

Respuesta :

Answer:

def build_dictionary(words):

   dic = {}

   for word in words:

       dic [word] =words.count(word)

   return(dic)

if __name__ == '__main__':

   words = input().split()

   your_dictionary = build_dictionary(words)

   sorted_keys = sorted(your_dictionary.keys())

   for key in sorted_keys:

       print(key + ': ' + str(your_dictionary[key]))

Explanation:

use the count function cause the initial input is a list.

Following are the Python program code to count the list of word in the dictionary:

Python Program to count list of words:

def build_dictionary(words):#defining the method build_dictionary that takes words as a parameter

   f = {}#defining a variable dictionary as a f

   for i in words:#defining a loop that checks dictionary value

       if (i in f):#defining an if block that check dictionary value

           f[i] += 1#addining 1 in the value of list

       else:#defining else block

           f[i] = 1#holding 1 in the list

   return f#return list value

if __name__ == '__main__':

   words = input().split()#defining a variable words that input value and splits its value

   your_dictionary = build_dictionary(words)#defining a variable your_dictionary that adds value in build_dictionary

   sorted_keys = sorted(your_dictionary.keys())#defining variable sorted_keys that sort value your_dictionary as key

   for k in sorted_keys:#defining loop that counts number of value

       print(k + ': ' + str(your_dictionary[k]))#defining print method that prints its value

Output:

Please find the attached file.

Program Explanation:

  • Defining the method "build_dictionary" that takes one parameter "words" in the method.
  • Inside this method, a dictionary variable named "f" is declared in which a for loop is declared.
  • Inside this, a conditional block is declared that counts words' value.
  • In the next step, a constructor is declared, in which words variable is declared that input value and splits its value.
  • In the next step, "your_dictionary" is declared that that adds value to build_dictionary, and use stored method is used that sort its value and store its value in "sorted_keys".
  • After that, a for loop is declared that counts the "sorted_keys" dictionary value and prints its counted list value.

Find out more information about the dictionary here:

brainly.com/question/1199071

Ver imagen codiepienagoya
ACCESS MORE
EDU ACCESS
Universidad de Mexico