I'm using SequenceMatcher to compare the output of usernames from an API list and an LDAP group. The intent is to add, and separately, remove users.
I've got the 'add' part working. I can't get the 'remove' part to give me the correct list of usernames.
All I'm doing is swapping around 'a' and 'b'. So if it works when computing 'how to I get from a to b', surely the same should apply if asking 'how do I get from b to a'?
Here's the content of my lists, with the username that exists in both highlighted.
a: ['user1', 'user2']
b: ['usera', 'userb', 'userc', 'userd', 'user1', 'userx', 'usery']
def compare_two_lists(a, b, action):
users_to_add = []
users_to_delete = []
if action == 'add':
s = SequenceMatcher(None, a, b)
for tag, i1, i2, j1, j2 in s.get_opcodes():
if tag in ('insert', 'replace'):
users_to_add.append(b[j1:j2])
print('Add:')
print(users_to_add)
if action == 'delete':
s = SequenceMatcher(None, b, a)
print('a:')
print(a)
print('b:')
print(b)
for tag, i1, i2, j1, j2 in s.get_opcodes():
if tag == 'delete':
users_to_delete.append(a[i1:i2])
print('Delete: ')
print(users_to_delete)
The output I get:
Delete: [['user1', 'user2']]
user1 absolutely should not be in this list, so why is it? user2 should be in this list but as far as I'm concerned it's only there as coincidence and not because SequenceMatcher is actually reporting it properly.
I've messed around with swapping i1:i2, j1:j2, a, b, about as many different variants as I can think of.