Question

Please write the python code for a function called called count_all_words that takes a phrase as...

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.

Homework Answers

Answer #1
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"))

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a python function called unique words that takes a phrase as an input string and...
Write a python function called unique words that takes a phrase as an input string and returns a list of the unique words found in that phrase. The words should be listed in alphabetical order.
Write a function called count_word that takes a phrase and a word as arguments and returns...
Write a function called count_word that takes a phrase and a word as arguments and returns a count of the number of occurrences of that particular word in the phrase. The function should return an integer. Python Script in ATOM or REPL
Write a function called most_common_word that takes a phrase as input and returns the most common...
Write a function called most_common_word that takes a phrase as input and returns the most common word in the phrase. If multiple words are equally common then return the first word alphabetically. Example: >>> phrase = "she sells sea shells by the sea shore" >>> unique_words(phrase) ['by', 'sea', 'sells', 'she', 'shells', 'shore', 'the'] >>>count_word(phrase, 'sea') 2 >>>count_all_words(phrase) [['by', 1], ['sea', 2], ['sells', 1], ['she', 1], ['shells', 1], ['shore', 1], ['the', 1]] >>>most_common_word(phrase) 'sea' Python Script for ATOM or REPL, (NOT...
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
Create a function called lists_Sum(lists) that takes in a list of lists, each element in each...
Create a function called lists_Sum(lists) that takes in a list of lists, each element in each sublist will be a number. Your function will return the product of all the numbers in the inner lists added together. solve for python EX.  lists_sum ([[2,2],[3,5],[5,8]]) returns 59
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
This is an intro to Python question: #Write a function called linear() that takes two parameters...
This is an intro to Python question: #Write a function called linear() that takes two parameters #- a list of strings and a string. Write this function so #that it returns the first index at which the string is #found within the list if the string is found, or False if #it is not found. You do not need to worry about searching #for the search string inside the individual strings within #the list: for example, linear(["bobby", "fred"], "bob") #should...
In Python Compose a function that takes an array of strictly positive floats as its parameter...
In Python Compose a function that takes an array of strictly positive floats as its parameter and rescales the array so that each element is between 0.0 and 1.0 by subtracting the minimum value from each element and then dividing each element by the difference between the minimum and maximum values in the array. The first constraint in this assignment is that you must use a list comprehension to implement the function. To test your function, have your program call...
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT