Skip to content

Instantly share code, notes, and snippets.

@filipecifali
Created October 6, 2016 15:21
Show Gist options
  • Save filipecifali/84df2e7b0a04bd6253ad59ece63014ff to your computer and use it in GitHub Desktop.
Save filipecifali/84df2e7b0a04bd6253ad59ece63014ff to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
Autor: [email protected]
Modulo builtin disponivel no core
https://docs.python.org/3/library/collections.html?highlight=counter#collections.Counter
"""
from collections import Counter
# declarar a lista para preencher ela com todas as palavras do arquivo
words = []
# usando with open ele mantem o arquivo aberto somente pela iteração do loop
with open('t.txt', 'r') as f:
# para cada linha lida executar a adição na lista
for line in f.readlines():
# rstrip remove a quebra de linha para não poluir a string que é armazenada na lista
words.append(line.rstrip('\n'))
# Majicks
c = Counter(words)
print("Top ocorrências: ", c.most_common(3))
print("Lista completa:", c)
string1
string3
string3
string2
string1
string2
string2
string1
string3
string3
string1
string3
string4
string2
string4
string1
string4
string2
string1
string2
string1
string3
string1
string3
string1
string3
string4
string3
string3
string1
string1
string4
string4
string1
string1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment