已实现用户输入收集,求创建并排序动态列表的列表方案
Hey there! Let's work through this problem step by step—you're already halfway there with the word collection part, so let's build out the dynamic list-of-lists and sorting functionality.
First, let's polish your word collection function
Your current wordList() is missing a few key pieces: adding valid words to the list, handling empty inputs, and finishing the prompt text. Here's a refined version:
def wordList(): unsortedList = [] promptUser = "" while promptUser != "stop": promptUser = input("Type words, one at a time. When you are done, type 'stop': ").strip().lower() # Skip empty inputs and the "stop" command itself if promptUser != "stop" and promptUser: unsortedList.append(promptUser) return unsortedList
Dynamic List-of-Lists Creation & Sorting
The goal here is to avoid pre-defining fixed empty lists (like [[],[],[]]) and instead create groups dynamically based on your words. A common use case is grouping by first letter, but I'll also show grouping by word length so you can adapt it to your needs.
Option 1: Group by first letter (sorted)
This approach uses a dictionary to map each starting character to a list of words, then converts that into a sorted list-of-lists:
def build_sorted_grouped_list(): # Get the user's words first words = wordList() # Dynamically create groups using a dictionary char_groups = {} for word in words: first_char = word[0] # Create a new empty list for this character if it doesn't exist yet if first_char not in char_groups: char_groups[first_char] = [] char_groups[first_char].append(word) # Sort each individual group alphabetically for char in char_groups: char_groups[char].sort() # Convert the dictionary values into a sorted list-of-lists (ordered by first letter) sorted_groups = sorted(char_groups.values()) return sorted_groups
Option 2: Group by word length (sorted)
If you want to group words by how long they are, the logic is almost identical—just use word length as the dictionary key:
def build_length_sorted_grouped_list(): words = wordList() length_groups = {} for word in words: word_length = len(word) if word_length not in length_groups: length_groups[word_length] = [] length_groups[word_length].append(word) # Sort each group alphabetically for length in length_groups: length_groups[length].sort() # Order groups by word length (smallest to largest) sorted_groups = [length_groups[length] for length in sorted(length_groups.keys())] return sorted_groups
Test it out!
Call the function to see it in action:
# Test letter-based grouping letter_result = build_sorted_grouped_list() print("Sorted groups by first letter:") for group in letter_result: print(group) # Or test length-based grouping length_result = build_length_sorted_grouped_list() print("\nSorted groups by word length:") for group in length_result: print(group)
Quick notes to adapt this:
- Case insensitivity: The
.lower()in the input step ensures "Apple" and "apple" get grouped together. Remove it if you want case-sensitive grouping. - Custom grouping logic: Swap out the key (like
word[0]orlen(word)) for any other rule—e.g., group by last letter withword[-1], or by vowel count.
内容的提问来源于stack exchange,提问作者MGinOttawa

