Complete the analyse_words() function that takes a single string parameter - text. The string text consists of one or more words, separated by white space. You can assume that there are no duplicate words in the text. The function goes through these words creating a dictionary where each item is a key-value pair:
Key - An integer representing how many vowels a word has.
Value - A list containing the words in text that have this many vowels. This list should be sorted in alphabetical order.
Note, that the function should be case insensitive and be able to handle uppercase and lowercase vowels. Some examples of the function being called are shown below.
For example:
Test Result
text = "The quick brown fox jumps over a lazy dog"
word_analysis_dict = analyse_words(text)
for key in sorted(word_analysis_dict):
print(key, word_analysis_dict[key])
1 ['The', 'a', 'brown', 'dog', 'fox', 'jumps', 'lazy']
2 ['over', 'quick']
text = "PETER PIPER picked a PECK of pickled peppers"
word_analysis_dict = analyse_words(text)
for key in sorted(word_analysis_dict):
print(key, word_analysis_dict[key])
1 ['PECK', 'a', 'of']
2 ['PETER', 'PIPER', 'peppers', 'picked', 'pickled']
视频信息
答案文本
视频字幕
Welcome to this explanation of the analyse_words function. This function takes a string of words as input and analyzes them based on their vowel count. It counts the number of vowels in each word, ignoring case, and groups words by their vowel count. The function returns a dictionary where each key is the number of vowels, and each value is an alphabetically sorted list of words with that vowel count. Let's look at the code implementation. The function first initializes an empty dictionary and a set of vowels. It then splits the input text into words, counts the vowels in each word, and groups them accordingly. Finally, it sorts each list of words alphabetically before returning the dictionary.
Let's walk through a concrete example. Consider the input text: 'The quick brown fox jumps over a lazy dog'. First, we split this text into individual words. Then, for each word, we count the number of vowels. For example, 'The' has one vowel, the letter 'e'. 'Quick' has two vowels, 'u' and 'i'. 'Brown' has one vowel, the letter 'o'. And so on for each word in the sentence. After counting vowels in all words, we group them by vowel count. Words with one vowel include 'The', 'a', 'brown', 'dog', 'fox', 'jumps', and 'lazy'. Words with two vowels include 'over' and 'quick'. Each group is sorted alphabetically. The function returns a dictionary where the keys are the vowel counts and the values are these sorted lists of words. When we print this dictionary by iterating through its sorted keys, we get the output shown on the right.
Let's look at another example that demonstrates the case insensitivity of our function. For the input 'PETER PIPER picked a PECK of pickled peppers', we count vowels regardless of case. For instance, 'PETER' has two vowels, both uppercase E's. 'PIPER' also has two vowels, uppercase I and E. 'picked' has one vowel, lowercase i. And so on. Notice that while vowel counting is case insensitive, the original case of each word is preserved in the output. The function returns a dictionary where words with one vowel include 'PECK', 'a', and 'of'. Words with two vowels include 'PETER', 'PIPER', 'peppers', 'picked', and 'pickled'. When sorted alphabetically, uppercase words come before lowercase words, which is why 'PECK' appears before 'a' in the output. This example clearly demonstrates how our function handles case insensitivity for vowel counting while preserving the original case in the output.
Let's walk through the key steps in our code implementation. The function can be broken down into three main steps. Step 1 is initialization. Here, we create an empty dictionary to store our results and a set containing the five vowels for efficient lookups. We also split the input text into individual words. Step 2 is processing each word. For each word, we iterate through its characters, counting vowels in a case-insensitive manner by converting each character to lowercase before checking if it's a vowel. If we encounter a new vowel count, we create a new entry in our dictionary with an empty list. Then we append the current word to the appropriate list based on its vowel count. Step 3 is sorting the results. We iterate through all the word lists in our dictionary and sort each one alphabetically. Finally, we return the completed dictionary. This implementation efficiently handles the requirements of being case insensitive for vowel counting while preserving the original case of words in the output.
To summarize what we've learned about the analyse_words function: This function takes a string of words and groups them by vowel count, creating a dictionary where the keys are the number of vowels and the values are lists of words. An important feature is that vowel counting is case-insensitive, meaning both uppercase and lowercase vowels are counted the same way. However, the original case of each word is preserved in the output. Within each group, words are sorted alphabetically, with uppercase words coming before lowercase words in standard alphabetical sorting. The function efficiently handles these requirements by using a set for vowel checking and dictionary operations for grouping. This implementation demonstrates good use of Python's built-in data structures and string handling capabilities.