1. Wörter zählen
LeichtSchreib eine Funktion count_words(text), die zählt, wie oft jedes Wort in einem Text vorkommt. Groß- und Kleinschreibung spielt keine Rolle.
Beispiel: count_words("Der Hund und der Ball") ergibt {"der": 2, "hund": 1, "und": 1, "ball": 1}
Tipp: text.lower().split() gibt dir die Wörter, ein Dictionary merkt sich die Anzahl.
Lösung anzeigen
def count_words(text):
counts = {}
for word in text.lower().split():
counts[word] = counts.get(word, 0) + 1
return counts
Kürzer geht es mit collections.Counter: Counter(text.lower().split()).