-1

I am trying to take two List of Lists and compare them against each other. From my research the closest I have found was this How to compare a list of lists/sets in python?. I am trying to do something just slightly different and may be heading down the wrong road and if so please feel free to point me in a better and more optimal direction. The first list will always be a baseline. It is what should be present in the second subset. So a huge bonus would be having the ability to know if the second list was missing the element or if the difference is due to a new element added to Second.

I have 2 Lists of lists I.E.

    First = [['1', 'a', 'b', 'c'], ['2', 'a', 'b', 'c', 'd']]
    Second = [['1', 'b', 'c'], ['2', 'a', 'b', 'c', 'd']]

The numbers are unique id's for the list. I could follow the above and get which ones are different as tuples but here is where the difference comes in. I am wanting the final result to retain the Unique id and then only the differences found. I.E Otherwise I am stuck searching hundreds of lines that have changed and could have changed in one of two places essentially doubling he search.

    diff[['1', 'a', 'missing from second']] 

the output can really be anything as long as I can retain unique id to data associated with it, and again the knowing whether it was missing or added in the second is more of a bonus but would help immensely. Also if tuples work better for this I could use the answer from the link above to use the map function.

for example of other way around

    First = [['1', 'a', 'b'], ['2', 'a', 'b', 'c', 'd']]
    Second = [['1', 'a', 'b', 'c'], ['2', 'a', 'b', 'c', 'd']]
    diff[['1', 'c' 'added to second']

so I suppose if I were to keep track of the when it was added it might be better to have the output be below for each example given above. Again this is just me spitballing because I am not sure what is the most optimal way to do this.

    diff_removed[['1', 'a']
    diff_added[['1','c'] 
Community
  • 1
  • 1
divdeamor
  • 89
  • 1
  • 2
  • 10
  • What if first was missing some elements? – thefourtheye May 15 '15 at 19:11
  • I will clarify the question above – divdeamor May 15 '15 at 19:19
  • You probably (definitely) have a bad data structure. I recommend researching all the available built-in types - `tuple`, `list`, `set`, `dict`, `object`, etc. - and redesigning your data structure to use appropriate types that will help you rather than hinder you. – TigerhawkT3 May 15 '15 at 19:26
  • @TigerhawkT3 thanks for the info I will indeed go educate myself on the built-in types available. At the same time could you provide a little more input? Like what you feel would be a more viable data-type and why? – divdeamor May 15 '15 at 19:36
  • I'm not familiar with the entire problem you're trying to solve, but in the limited example of `First` and `Second`, those should be dictionaries, with the unique ID being the key and a list of the elements connected to the ID being the value. `first = {'1':['a', 'b'], '2':['a', 'b', 'c', 'd']}` – TigerhawkT3 May 15 '15 at 19:40
  • @TigerhawkT3 Awe thanks, it makes sense now that you say it. I guess even after reading and figuring out a dictionaries I never connected the dots that would of let me have the value portion be a list instead of a single value. – divdeamor May 15 '15 at 19:48

1 Answers1

1
first = {'1':['a', 'b', 'c'], '2':['a', 'b', 'c', 'd']} # the first dictionary
second = {'1':['b', 'c'], '2':['a', 'b', 'c', 'd']} # the second dictionary
result = {} # initialize a result dictionary
for key in first: # go through the keys in one of them
    # look into dictionary indexing vs the get() method
    f = set(first[key]) # make a set out of this key's values for first
    s = set(second[key]) # make a set out of this key's values for second
    # add this entry to the result -
    # will produce key:(elements in first for this ID that are not in second,
    #                   elements in second for this ID that are not in first)
    result[key] = (f-s, s-f)

This will produce a result dictionary containing {'2': (set(), set()), '1': ({'a'}, set())}. You can then do additional processing if you want to change the empty sets to None, or feed this into a pretty-print function, or sort it, or whatever.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • Thanks for your help and for not only showing examples but guiding me to a better solution to my problem. I have to make a few more tweaks to it for what i need but I believe i can fumble my way through thanks to this. – divdeamor May 15 '15 at 20:23