Files
Laboratori-PY/Laboratorio11/countWords.py
2024-03-22 17:01:42 +01:00

40 lines
787 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
def clean(word: str):
a = len(word)
while True:
word = word.strip("()[]{}!?.,<>:;-_`")
if a == len(word):
break
a = len(word)
return word
def main():
filename = input("Inserisci il nome del file: ")
fio = open(filename, "r")
words = {}
for line in fio:
for word in line.split():
word = clean(word)
if word not in words:
words[word] = 1
else:
words[word] += 1
fio.close()
vals = sorted(words.values(), reverse=True)
for i in range(0, 100):
for word in words:
if words[word] == vals[i]:
print(word, end=" ")
words.pop(word)
break
print()
main()