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
