Compare zip lists where order is important
Frank Millman
frank at chagford.com
Thu Aug 29 02:37:49 EDT 2019
On 2019-08-29 5:24 AM, Sayth Renshaw wrote:
> Hi
>
> Trying to find whats changed in this example. Based around work and team reschuffles.
>
> So first I created my current teams and then my shuffled teams.
>
> people = ["Tim","Bill","Sally","Ally","Fred","Fredricka"]
> team_number = [1,1,2,2,3,3]
>
> shuffle_people = ["Fredricka","Bill","Sally","Tim","Ally","Fred"]
> shuffle_team_number = [1,1,2,2,3,3]
>
> Then combine.
>
> teams = list(zip(people,team_number))
> shuffle_teams = list(zip(shuffle_people, shuffle_team_number))
>
> Then I am attempting to compare for change.
>
> [i for i, j in zip(teams, shuffle_teams) if i != j]
>
> #Result
> [('Tim', 1), ('Ally', 2), ('Fred', 3), ('Fredricka', 3)]
>
> #Expecting to see
>
> [('Fredricka', 1),('Tim', 2)]
>
> What's a working way to go about this?
>
This would have worked if you sorted your lists first -
>>> [i for i, j in zip(sorted(teams), sorted(shuffle_teams)) if i != j]
[('Ally', 2), ('Fredricka', 3), ('Tim', 1)]
Except you wanted to see the results from shuffle_teams, so instead of
'i for i, j ...', use 'j for i, j ...' -
>>> [j for i, j in zip(sorted(teams), sorted(shuffle_teams)) if i != j]
[('Ally', 3), ('Fredricka', 1), ('Tim', 2)]
Frank Millman
More information about the Python-list
mailing list