0
I need a function that receives the result of a word counting function and based on these values, this function calculates the average of how many times a word appears in the text, stores that average in a dictionary and returns the dictionary with that value.
example:
Parâmetro: ‘{‘três’: 2, ‘pratos’:1}
Retorno: {'três': 0.25, 'pratos': 0.125}
I was able to do the function counts words, but I’m having difficulty calculating the average in which each word appears.
def conta_palavras(x):
palavras = {}
for palavra in x.split():
if palavra in palavras:
palavras[palavra] += 1
else:
palavras[palavra] = 1
return palavras
def conta_media_palavras(x):
palavras = conta_palavras
maximo = 0
freq = ''
for palavra in palavras:
if palavras[palavra] > maximo:
maximo = palavras[palavra]
freq = palavra
Your question is not well formulated and we still lack the data to better understand the situation. What text are you talking about before displaying the parameter?. Anyway, try using the function
count()
to count the words.– WhoisMatt
Victor, as Whoismatt said, your question is really poorly worded, so try to edit it and put in some more presentable code so we can help you. The more detailed the question, the easier it is to get help.
– Robson Silva