Code: Alles auswählen
# -*- coding: cp1252 -*-
def count_words(text):
"""gibt aus, wie oft jedes Wort im String 'text' vorkommt"""
for i in ["'", ",", ".", ";", ":", "?", "!", "(", ")"]:
text = text.lower().replace(i, "")
text = text.replace("\n", " ")
words = text.split(" ")
while True:
if "" in words: words.remove("")
else: break
result = []
counted_words = []
for i in words:
if not i in counted_words:
result.append(i+" : "+str(words.count(i)))
counted_words.append(i)
return "\n".join(result)
Code: Alles auswählen
print(count_words("""Fischers Fritz fischt frische Fische, frische Fische fischt Fischers Fritz.
eins; zwei, zwei; drei, drei, drei."""))
>>>
fischers : 2
fritz : 2
fischt : 2
frische : 2
fische : 2
eins : 1
zwei : 2
drei : 3