如何移除列表中的增量元素并将其合并压缩为单个元素
Solution to Group Entries by Consecutive Numbers
Hey there! Let's break down how to convert your entry list into the grouped range format you need. I'll keep this straightforward since you're still building your programming skills.
Step 1: Understand the Problem
We need to:
- Identify consecutive entry numbers (like 3 and 4, or 6-9)
- Merge them into a single "Entry #X - #Y" string
- Preserve any extra info (like the (2019) from Entry #3) alongside the range
Step 2: Python Code Implementation
Here's a complete, commented solution using regular expressions to parse entries and grouping logic to merge consecutive numbers:
import re def group_consecutive_entries(entries): # First, parse each entry to extract number and extra suffix parsed_entries = [] for entry in entries: # Use regex to pull out the number and any trailing text (like (2019)) match = re.match(r'Entry #(\d+)(.*)', entry) if match: num = int(match.group(1)) # Strip extra whitespace from the suffix suffix = match.group(2).strip() parsed_entries.append((num, suffix)) if not parsed_entries: return [] # Now group consecutive numbers groups = [] # Start with the first entry as the initial group start_num, start_suffix = parsed_entries[0] end_num = start_num for num, suffix in parsed_entries[1:]: # Check if current number is consecutive to the end of the current group if num == end_num + 1: end_num = num else: # Add the current group to the list groups.append((start_num, end_num, start_suffix)) # Start a new group start_num, start_suffix = num, suffix end_num = num # Don't forget to add the last group groups.append((start_num, end_num, start_suffix)) # Convert groups back to the desired string format result = [] for start, end, suffix in groups: if start == end: # Single entry, keep original format result.append(f"Entry #{start}{' ' + suffix if suffix else ''}") else: # Range of entries, append suffix from the first entry in the group result.append(f"Entry #{start} - #{end}{' ' + suffix if suffix else ''}") return result # Test with your example entries entries = ['Entry #1', 'Entry #3 (2019)', 'Entry #4', 'Entry #6', 'Entry #7', 'Entry #8', 'Entry #9'] grouped_entries = group_consecutive_entries(entries) print(grouped_entries) # Output: ['Entry #1', 'Entry #3 - #4 (2019)', 'Entry #6 - #9']
Step 3: Explanation of Key Parts
- Regex Parsing: The
re.matchline extracts the numeric part of each entry and any extra text (like(2019)). This works even if entries have varying suffixes. - Grouping Logic: We iterate through parsed entries and check if each number is consecutive to the last number in the current group. If yes, we extend the group; if not, we finalize the current group and start a new one.
- String Formatting: For each group, we either keep the single entry format or create a range string, adding the suffix from the first entry in the group (which matches your example where
(2019)stays with the 3-4 range).
Step 4: Test It Out
Copy the code into a Python file or notebook, run it, and you'll see it produces exactly the output you want. You can also adjust it if your entry format changes slightly (like different prefixes) by modifying the regex pattern.
内容的提问来源于stack exchange,提问作者RealPataka
相关产品推荐
相关产品推荐

