4
I need to count the occurrences of words in a text using as reference a list of predetermined words. I turned the text into a list of string
, the words are in a set (set
)
palavras_procuradas = {'de', 'solução', 'mesa', 'teste', 'acabaxi'}
texto = ['para', 'validar', 'minha', 'solução', 'foi', 'preciso', 'fazer', 'o', 'teste', 'de', 'mesa', 'do', 'algoritmo', 'elaborado', 'só', 'depois', 'do', 'teste', 'de', 'mesa', 'que', 'o', 'programa', 'foi', 'implementado', 'essa', 'estratégia', 'poupou', 'bastan', 'te', 'tempo', 'de', 'desenvolvimento']
dicionario = {}
for palavra in texto:
if palavra in palavras_procuradas:
dicionario[palavra]= palavra += 1
for chave in dicionario:
print (chave + " " + dicionario[chave])
The right way out would be:
acabaxi 0
teste 2
mesa 2
solução 1
de 3
On the output of my Code is giving error:
dicionario[palavra]= palavra += 1
^
SyntaxError: invalid syntax
Hello Wictor Keys, that the word pineapple does not appear in the text but it should be returned with value 0.
– Bruno
All right, I’ll make the change and put it on
– Wictor Chaves
Edited with the desired change :)
– Wictor Chaves
Hello Wictor Keys, thank you very much friend, your solution worked perfectly, if I get to the level of programming next to yours is already good for me. Thanks
– Bruno
I hope you get further than me :)
– Wictor Chaves