Question

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.

Homework Answers

Answer #1

Python code:

#defining unique_words function
def unique_words(phrase):
    #creating a list unique to store unique words
    unique=[]
    #looping each word in phrase
    for i in phrase.split():
        #checking if it is not in unique list
        if i not in unique:
            #adding it to unique list
            unique.append(i)
    #returning the sorted list
    return(sorted(unique))
#calling unique_words function and testing for sample phrase
print(unique_words("hi i am rahul i am from india"))

Screenshot:


Output:

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
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.
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...
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
Python Write function words() that takes one input argument—a file name—and returns the list of actual...
Python Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
PYTHON: Write a function called keepShouting. This function does not need any input parameters. The function...
PYTHON: Write a function called keepShouting. This function does not need any input parameters. The function should simply repeat whatever the user enters but in all capitals! The function should ask the user to enter a phrase. The phrase then should be capitalized and printed to the screen. If the user just hits return (which will produce the empty string) then the program should stop. Hint: Use a while loop and string methods # keepShouting() """ The function call should...
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...
Write a function that takes as input an English sentence (a string) and prints the total...
Write a function that takes as input an English sentence (a string) and prints the total number of vowels and the total number of consonants in the sentence. The function returns nothing. Note that the sentence could have special characters such as dots, dashes, and so on. write a python program please.
** Language Used : Python ** PART 2 : Create a list of unique words This...
** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except that it only accepts the specified type of input. The function takes two arguments: r prompt: str r type: int, float, str The function will keep prompting for input until correct input of the specified type is entered. The function returns the input. If the input was specified to be a number ( float or int), the value returned will be of the correct...
Python Write function stringCount() that takes two string inputs—a file name and a target string— and...
Python Write function stringCount() that takes two string inputs—a file name and a target string— and returns the number of occurrences of the target string in the file. >>> stringCount('example.txt', 'line') 4