如何使用预定义列表分割列表生成子列表或字典
Split List by Unique Predefined Titles into Sublists or Dictionary
Got it, let's work through this problem step by step. Since every title in your titles list shows up exactly once in x, we can build clean, straightforward solutions for both the sublist and dictionary outputs you're targeting.
Solution 1: Generate Sublist Output (new_list)
We'll iterate through x, keep track of the current group of elements, and whenever we hit a title from titles, we wrap up the current group (if it exists) and start a new one with the title. Here's the code:
titles = ['a','c','g','final'] x = ['a',3,4,2,'c','b','b1',2,'g','final', 5, 6, 'end'] new_list = [] current_group = [] for item in x: if item in titles: # If we have an ongoing group, add it to the result before starting fresh if current_group: new_list.append(current_group) # Start a new group with the current title current_group = [item] else: # Add non-title items to the current group current_group.append(item) # Don't forget to add the last group after the loop ends if current_group: new_list.append(current_group) print(new_list) # Output: [['a', 3, 4, 2], ['c', 'b', 'b1', 2], ['g'], ['final', 5, 6, 'end']]
Breakdown:
- We start with empty containers for the final result (
new_list) and the group we're building (current_group). - For each item in
x:- If it's a title, we check if
current_grouphas content from the previous section. If yes, we add it tonew_list, then resetcurrent_groupto start with the current title. - If it's not a title, we just append it to the ongoing
current_group.
- If it's a title, we check if
- After the loop finishes, we add the last remaining
current_grouptonew_list(since there's no title left to trigger the append inside the loop).
Solution 2: Generate Dictionary Output (my_dict)
For the dictionary version, we'll track which title we're collecting elements for, then map each title to its following elements. Here's how to implement it:
titles = ['a','c','g','final'] x = ['a',3,4,2,'c','b','b1',2,'g','final', 5, 6, 'end'] my_dict = {} current_title = None for item in x: if item in titles: # Save the previous title's elements to the dict if we were tracking one if current_title is not None: my_dict[current_title] = current_elements # Update to the new title and start an empty elements list current_title = item current_elements = [] else: # Add non-title items to the current title's element list current_elements.append(item) # Save the last title and its elements to the dict if current_title is not None: my_dict[current_title] = current_elements print(my_dict) # Output: {'a': [3, 4, 2], 'c': ['b', 'b1', 2], 'g': [], 'final': [5, 6, 'end']}
Breakdown:
- We initialize an empty dictionary (
my_dict), plus variables to track the current title we're working with (current_title) and its associated elements (current_elements). - For each item in
x:- If it's a title, we first save the previous title's elements to the dictionary (if there was one), then set
current_titleto this new title and resetcurrent_elementsto an empty list. - If it's not a title, we append it to
current_elements.
- If it's a title, we first save the previous title's elements to the dictionary (if there was one), then set
- After the loop, we save the last title and its elements to the dictionary.
Edge Cases Handled:
- Titles with no following elements (like
'g'in your example) will map to an empty list, which matches your expected output. - The last title can have any number of trailing elements, which will all be captured in its value list.
内容的提问来源于stack exchange,提问作者hadesfv
相关产品推荐
相关产品推荐

