Please write the python code for a function called called count_all_words that takes a phrase as an argument and counts each unique word in the phrase. The function should return a list of lists, where each sub-list is a unique [word, count] pair. (See example below.) Hint: A well-written list comprehension can solve this in a single line of code, but this approach is not required.
def count_all_words(phrase): d = {} for x in phrase.split(): if not d.get(x): d[x] = 1 else: d[x] += 1 result = [] for key in d: result.append([key, d[key]]) return result # Testing print(count_all_words("yes it is yes it is is is"))
Get Answers For Free
Most questions answered within 1 hours.