Define a function first_word(arg1),with a string argument arg1. In the function:a. use split() function to separate arg1 by "," into a list of wordsb. Return res that is the last element of the list of words
Question
Define a function first_word(arg1),with a string argument arg1. In the function:a. use split() function to separate arg1 by "," into a list of wordsb. Return res that is the last element of the list of words
Solution
Sure, here is the Python function based on your requirements:
def first_word(arg1):
# a. use split() function to separate arg1 by "," into a list of words
words = arg1.split(",")
# b. Return res that is the last element of the list of words
res = words[-1]
return res
This function takes a string arg1, splits it into a list of words separated by commas, and then returns the last word in the list.
Similar Questions
Have the function LongestWord(sen) take the sen parameter being passed and return the longest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty. Words may also contain numbers, for example "Hello world123 567"
Write a Python function that takes a list of words and return the longest word and the length of the longest one.
Write a Python program to split a string to words
Write a function repeat_word_count(text, n) that takes a string text and a positive integer n, converts text into a list of words based on simple whitespace separation (with no removal of punctuation or changing of case), and returns a sorted list of words that occur n or more times in text. For example:>>> repeat_word_count("buffalo buffalo buffalo buffalo", 2)['buffalo']>>> repeat_word_count("one one was a racehorse two two was one too", 3)['one']>>> repeat_word_count("how much wood could a wood chuck chuck", 1)['a', 'chuck', 'could', 'how', 'much', 'wood']
Write a function top5_words(text) that takes a single argument text (a non-empty string), tokenises text into words based on whitespace (once again, without any stripping of punctuation or case normalisation), and returns the top-5 words as a list of strings, in descending order of frequency. If there is a tie in frequency at any point, the words with the same frequency should be sub-sorted alphabetically (e.g. if 'turtle' and 'grok' both occur 5 times, 'grok' should come first). If there are less than five distinct words in text, the function should return all words in descending order of frequency (with the same tie-breaking mechanism). For example:>>> top5_words("one one was a racehorse two two was one too")["one", "two", "was", "a", "racehorse"]>>> top5_words("buffalo buffalo buffalo chicken buffalo")["buffalo", "chicken"]>>> top5_words("the quick brown fox jumped over the lazy dog")["the", "brown", "dog", "fox", "jumped"]
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.