[SOLVED] Re: Compare zip lists where order is important

Peter Otten __peter__ at web.de
Thu Aug 29 02:14:47 EDT 2019


Sayth Renshaw wrote:

> On Thursday, 29 August 2019 14:03:44 UTC+10, Sayth Renshaw  wrote:
>> On Thursday, 29 August 2019 13:53:43 UTC+10, Sayth Renshaw  wrote:
>> > On Thursday, 29 August 2019 13:25:01 UTC+10, 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?
>> > > 
>> > > Sayth
>> > 
>> > It looks like Tuples are comparing by position changes not content
>> > changes.
>> > 
>> > So this fails too
>> > 
>> > set(shuffle_teams) & set(teams)
>> > # {('Bill', 1), ('Fred', 3), ('Sally', 2)}
>> 
>> Well this works although its not clear which line is the change.
>> 
>> set(teams).symmetric_difference(set(shuffle_teams))
>> 
>> {('Ally', 2),
>>  ('Ally', 3), # This is the change Ally changed from 2 to 3
>>  ('Fredricka', 1), # However here this first line is the change.
>>  ('Fredricka', 3),
>>  ('Tim', 1),
>>  ('Tim', 2)}
>> 
>> Hints?
> 
> set(shuffle_teams).difference(set(teams))
> {('Ally', 3), ('Fredricka', 1), ('Tim', 2)}

(0) Sets use the comparison provided by their elements, there is no 
difference betwen ("Ally", 2) == ("Ally", 3) and "whatever" == 123
(1) If set operations work that means that list order is *not* important
(2) a.difference(b), which can also be written as a - b, will find the added 
pairs, but ignore the removed ones. Is that what you want?




More information about the Python-list mailing list