1.Word frequency
BeginnerCount how many times each word appears in text = "red blue red green blue red", then print each word and its count in alphabetical order.
Show hint
counts[word] = counts.get(word, 0) + 1 adds one, starting from 0 for a new word.
Show solution
text = "red blue red green blue red"
counts = {}
for word in text.split():
counts[word] = counts.get(word, 0) + 1
for word in sorted(counts):
print(word, counts[word])blue 2
green 1
red 3collections.Counter(text.split()) does the same counting in one line, and .most_common() sorts by count.