如何在Python中分离并排序以0为分隔符的数字序列?
Fixing the Segment Sorting and Concatenation Problem
Hey there! Let's break down what's going wrong with your current code and build a solution that matches your expected output.
First, Clarify the Core Requirements
Your goal is to:
- Split the integer list into segments separated by
0s - Sort each segment in ascending order
- Concatenate all sorted segments into a single integer (ignoring the
0separators)
What's Wrong with the Original Code?
Your existing sort function has two critical issues:
- Incorrect sorting logic: The selection sort implementation is buggy—initializing
minpos = 1skips the first element, and the hardcodedrange(i,6)only processes the first 6 elements of the list. - No segment splitting: The function sorts the entire list directly, not splitting it by
0s first. This is why your output has0s mixed in with sorted numbers, which doesn't match the requirement.
The Correct Solution
Here's a step-by-step implementation that meets your needs:
def process_nums(nums): # Step 1: Split the list into segments separated by 0s segments = [] current_segment = [] for num in nums: if num == 0: # Only add non-empty segments to avoid empty entries from consecutive 0s if current_segment: segments.append(current_segment) current_segment = [] else: current_segment.append(num) # Add the last segment if the list doesn't end with 0 if current_segment: segments.append(current_segment) # Step 2: Sort each segment and concatenate into a string result_str = "" for seg in segments: # Sort the segment in ascending order sorted_seg = sorted(seg) # Convert each number to string and join the segment result_str += ''.join(str(num) for num in sorted_seg) # Convert to integer (return 0 if there are no valid segments) return int(result_str) if result_str else 0 # Test with your input nums = [5,9,5,6,5,6,0,1,5,9,4,6,6,0,5,6] print(process_nums(nums)) # Output: 55566914566956
How This Works:
- Segment Splitting: We iterate through the list, building segments until we hit a
0. We skip empty segments (from consecutive0s) to keep things clean. - Sorting: Using Python's built-in
sorted()function is more reliable and efficient than writing custom selection sort. It returns a new sorted list for each segment. - Concatenation: Each sorted segment is converted to a string, joined together, and finally turned into an integer (or returns
0if there are no valid numbers).
Optional: Fixing the Custom Sort Function (If You Want to Use It)
If you specifically want to use a custom selection sort instead of sorted(), here's the corrected version for sorting a single segment:
def custom_sort(segment): n = len(segment) for i in range(n): minpos = i # Initialize minpos to current index, not 1 for j in range(i, n): # Use the segment's length, not hardcoded 6 if segment[j] < segment[minpos]: minpos = j # Swap elements segment[i], segment[minpos] = segment[minpos], segment[i] return segment
You can replace sorted(seg) with custom_sort(seg) in the process_nums function if you prefer this approach.
内容的提问来源于stack exchange,提问作者Rina Lia
相关产品推荐
相关产品推荐

