user739807
user739807

Reputation: 855

python, detecting elements have been removed/added/changed positions in a list

I have a list of items.. An orginal list and a modified list - what i want to know is which elements have been removed/added from the modified list and what position w.r.t to the original list. The lists do not have duplicates and are not sorted because the ordering of the items in the list matters. Take an example

Org = ['AMEND', 'ASTRT', 'ETIME', 'OBJ', 'ast', 'bias', 'chip', 'cold']
mod = ['AMEND', 'ASTRT',         'OBJ', 'ast', 'bias', 'chip', 'cold', 'flat', 'deb']

in the mod_list 'ETIME', was removed and 'flat', deb' were added - so the result of this would be 'ETIME' removed at index 2 and 'flat', deb' added at index 8 & 9.

My other problem is detecting if items have changed position. In the example below 'OBJ', and 'ASTRT' have changed positions.

Org = ['AMEND', 'ASTRT', 'ETIME', 'OBJ', 'ast', 'bias', 'chip', 'cold']
mod = ['AMEND', 'OBJ', 'ETIME', 'ASTRT', 'ast', 'bias', 'chip', 'cold']

Any ideas on how to solve this!

Upvotes: 4

Views: 226

Answers (1)

fraxel
fraxel

Reputation: 35269

You can use difflib to do this kind of thing:

>>> import difflib
>>> Org = ['AMEND', 'ASTRT', 'ETIME', 'OBJ', 'ast', 'bias', 'chip', 'cold']
>>> mod = ['AMEND', 'ASTRT', 'OBJ', 'ast', 'bias', 'chip', 'cold', 'flat', 'deb']
>>> list(difflib.ndiff(Org, mod))
['  AMEND', '  ASTRT', '- ETIME', '  OBJ', '  ast', '  bias', '  chip', '  cold', '+ flat', '+ deb']

From here you can iterate through the list and check if items are new or moved or missing. For example:

Org = ['a', 'b' ,'c', 'd', 'e', 'f', 'g', 'h', 'i']
mod = ['i', 'b', 'c', 'z', 'd', 'f', 'g', 'h', 'a']
differences = set(difflib.ndiff(Org, mod))

moved = set([item[2:] for item in differences if item[0]=='+' and '-' + item[1:] in differences])
removed = set([item[2:] for item in differences if item[0]=='-']) - moved
added = set([item[2:] for item in differences if item[0]=='+']) - moved

print 'moved items:', moved
print 'removed items:', removed
print 'added items:', added
#output:
moved items: set(['a', 'i'])
removed items: set(['e'])
added items: set(['z'])

Upvotes: 9

Related Questions