feat: Initial commit
This commit is contained in:
40
Laboratorio11/countWords.py
Normal file
40
Laboratorio11/countWords.py
Normal file
@@ -0,0 +1,40 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user