Search code examples
pythondjangolistdjango-querysetarray-merge

python merge two query sets and addition of matching records


I have a need to merge two querysets together with simalar data.

Set 1 (stats) has a list of teams, total points for that team and the division the team is in.

Set 2 (spares) has a list of teams that have had a member spare for them which includes the teams and points (no division for this set).

What I would like to do is merge the querysets into one and when the teams match, just add the spares points to the team total, keeping the team name and division.

The code i am using below will sort of do what i am looking for except it does not include the teams that never had a spare on it.

Consider the following data.

Stats List

  • Team 1 : 8 Points : Division A
  • Team 2 : 3 Points : Division B
  • Team 3 : 7 Points : Division A
  • Team 4 : 5 Points : Division B
  • Team 5 : 4 Points : Division A

Spares List

  • Team 1 : 3 points
  • Team 3 : 6 Points

So what I want to do is merge these two lists where the teams match but total their points. In this case the final list would look like this:

Final List

  • Team 1 : 11 Points : Division A
  • Team 2 : 3 Points : Division B
  • Team 3 : 13 Points : Division A
  • Team 4 : 5 Points : Division B
  • Team 5 : 4 Points : Division A

Where team 1 and 3 have new totals added from the spares list.

The code i am using now that is not working for me is close to what I want

full_stats = {}
for stat in stats:
    for spare in spares:
        if stat['team'] == spare['team']:
            tot_points = int(stat['points']) + int(spare['points'])
            full_stats.append([stat['team'],tot_points],stat['division'])
            break
        else:
            continue

full_stats will show me only teams that have had spares (Team 1 and 3)

If I add full_stats.append([stat['team'],stat['points'],stat['division']) to the else, it will duplicate the data. So it will show me 7 items. Which are the 5 original team points from the stats list and then the two new totals for team 1 and team 3.

I have know I have a logic misunderstanding here but I am unable to figure out how to get these to add and only show me the 5 teams with the duplicates from the spares list added to stats list with the team of the same name.

I am doing this in a django view and will present the results of full_stats to the template.

Any help would be appreciated.

Thanks, jAC


Solution

  • @Alan-hover answer was close to what i wanted but I did not want the second tuple there to iterate through. However, separating the for loops instead of trying to do it in a nested fashion had given me the right direction to take this in. Thanks Alan.

    Here is the code change I made to make this work for me.

    full_stats = []
    
    for stat in stats:
        full_stats.append({'team': stat['team'],'points': stat['points']})
    
    for stat in full_stats:
        for spare in spares:
            if stat['team'] == spare['team']:
                total_points = int(stat['points']) + int(spare['points'])
                stat['points'] = total_points