如何将两个字典列表合并为字典:以指定字典值为键分组元素
Let's break down how to solve this problem—we need to evenly distribute the students from lst1 into the teams defined in lst2, then structure the result into a dictionary where each team name is a key pointing to its assigned students.
Step-by-Step Solution (Even Split Case)
First, let's cover the core scenario where the number of students is perfectly divisible by the number of teams (like your example with 4 students and 2 teams). Here's a clean, readable implementation:
lst1 = [{'st_name': 'ram', 'st_email_id': 'ram@abc.com'}, {'st_name': 'Raj', 'st_email_id': 'raj@abc.com'}, {'st_name': 'jatin', 'st_email_id': 'jatin@abc.com'}, {'st_name': 'tom', 'st_email_id': 'tom@abc.com'}] lst2 = [{'team_name': 'Team1'}, {'team_name': 'Team2'}] # Calculate how many students each team gets total_students = len(lst1) total_teams = len(lst2) students_per_team = total_students // total_teams # Split the student list into equal-sized groups student_groups = [lst1[i * students_per_team : (i + 1) * students_per_team] for i in range(total_teams)] # Map each team to its student group (convert team name to lowercase as in your desired output) final_result = {team['team_name'].lower(): group for team, group in zip(lst2, student_groups)} print(final_result)
Output
Running this code will give you exactly the format you need:
{ "team1": [ {'st_name': 'ram', 'st_email_id': 'ram@abc.com'}, {'st_name': 'Raj', 'st_email_id': 'raj@abc.com'} ], "team2": [ {'st_name': 'jatin', 'st_email_id': 'jatin@abc.com'}, {'st_name': 'tom', 'st_email_id': 'tom@abc.com'} ] }
Handling Uneven Student Counts
If you ever have a scenario where the number of students isn't perfectly divisible by the number of teams (e.g., 5 students and 2 teams), we can adjust the code to distribute the extra students evenly across the first few teams. Here's a more robust version using itertools.islice:
from itertools import islice lst1 = [{'st_name': 'ram', 'st_email_id': 'ram@abc.com'}, {'st_name': 'Raj', 'st_email_id': 'raj@abc.com'}, {'st_name': 'jatin', 'st_email_id': 'jatin@abc.com'}, {'st_name': 'tom', 'st_email_id': 'tom@abc.com'}, {'st_name': 'sarah', 'st_email_id': 'sarah@abc.com'}] lst2 = [{'team_name': 'Team1'}, {'team_name': 'Team2'}] def split_uneven_list(lst, num_groups): it = iter(lst) base_size = len(lst) // num_groups extra = len(lst) % num_groups for i in range(num_groups): # Assign one extra student to the first 'extra' groups group_size = base_size + (1 if i < extra else 0) yield list(islice(it, group_size)) total_teams = len(lst2) student_groups = list(split_uneven_list(lst1, total_teams)) final_result = {team['team_name'].lower(): group for team, group in zip(lst2, student_groups)} print(final_result)
Quick Notes
- We convert team names to lowercase with
.lower()to match your sample output—if you want to keep the original capitalization (e.g., "Team1" instead of "team1"), just remove that method call. - The first solution works for clean even splits, while the second handles edge cases where student counts don't divide evenly by team numbers.
内容的提问来源于stack exchange,提问作者user14794753

