Python 3中如何根据key值对连续value进行分组?
Hey there! Let's work through this problem together since you're new to Python. First, let's clarify what we need to achieve based on your input and desired output:
Your input is a list of (value, key) tuples:
input_list = [(5, 1), (0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (6, 3)]
And you want the output: 0,1,2 3,4,6 5
From your explanation, it seems the rules are:
- Values whose corresponding key occurs only once get their own group (like
5with key1). - For keys that occur multiple times, split their sorted values into equal-sized subgroups (key
3occurs 6 times, so split its sorted values into two groups of 3). - Finally, sort all groups by their smallest value to get the final order.
Step-by-Step Implementation
Here's how to code this in Python, with explanations:
1. Import Required Module
We'll use defaultdict from the collections module to easily group values by their key:
from collections import defaultdict
2. Group Values by Key
First, we'll organize all values into lists grouped by their corresponding key:
input_list = [(5, 1), (0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (6, 3)] # Create a dictionary to hold key-to-values mappings key_groups = defaultdict(list) for value, key in input_list: key_groups[key].append(value)
This gives us:
key_groups[1] = [5]key_groups[3] = [0, 1, 2, 3, 4, 6](order might vary initially, but we'll sort next)
3. Sort Values in Each Key Group
Next, we sort the values in each key group to ensure we can split them logically:
for key in key_groups: key_groups[key].sort()
Now key_groups[3] becomes [0, 1, 2, 3, 4, 6].
4. Split Large Groups into Subgroups
For keys with multiple values, we split their sorted list into equal-sized subgroups. In your case, key 3 has 6 values, so we split into two groups of 3:
final_groups = [] for values in key_groups.values(): group_size = len(values) # If the group size is even, split into two equal parts; adjust logic if needed for other cases if group_size > 1 and group_size % 2 == 0: half = group_size // 2 final_groups.append(values[:half]) final_groups.append(values[half:]) else: final_groups.append(values)
5. Sort Groups by Their Smallest Value
To get the order you want (0,1,2 first, then 3,4,6, then 5), we sort the final groups based on the smallest value in each group:
final_groups.sort(key=lambda group: min(group))
6. Format the Output
Finally, convert each group to a comma-separated string and join them with spaces:
output = ' '.join([','.join(map(str, group)) for group in final_groups]) print(output) # Output: 0,1,2 3,4,6 5
Notes on Flexibility
If your actual rules are slightly different (like grouping by the count of key occurrences instead of splitting large groups), we can adjust the code! For example, if you wanted all values from keys that occur 3 times in one group, we'd modify the grouping logic to group by the occurrence count instead of splitting. But based on your input and desired output, this code should work perfectly.
内容的提问来源于stack exchange,提问作者ReeceAPoole

