I found pretty much answers getting difference between 2 lists but couldn't able to find matrix lists.
I have 2 lists like below and I want to get the members which exist in "list1's first indice" and not exist in "list2's first indice"
I want to compare only the first indices of the list members and find the difference.
list1 = [(12, 'Text1'), (123, 'Text2'), (22, 'Text3')]
list2 = [('22', 'company1'), ('232', 'company2'), ('233', 'company3')]
(Not sure why list2's first indices has '' characters, couldn't able to remove it but I think still can be comparable.)
I'm using the code below but it takes hours(both indices has ~5000 members).
Is there any other way to calculate&find fastly?
notExist = []
for i,j in list1:
for x,y in list2:
if i == x:
try:
notExist.remove((i,j))
except:
print("Not Exist")
break
else:
if i not in notExist:
notExist.append((i,j))
CodePudding user response:
Convert them to dictionaries with key: (index, value) pairs:
list1 = [(12, 'Text1'), (123, 'Text2'), (22, 'Text3')]
list2 = [('22', 'company1'), ('232', 'company2'), ('233', 'company3')]
# convert these keys to strings, it'll make comparing a bit easier
d1 = {str(k): (idx, v) for idx, (k, v) in enumerate(list1)}
d2 = {k: (idx, v) for idx, (k, v) in enumerate(list2)}
# Now compare which dictionary has what
keys = ("12", "123", "22", "232")
results = []
for k in keys:
# try to see if value is in the dictionary
res = d1.get(k)
# if it isn't, skip it
if not res:
continue
# if it is, go ahead and test if it's in the other
if d2.get(k):
# skip if found
continue
# otherwise, append the key, index, and value
results.append((k, *res))
CodePudding user response:
Convert both keys lists to sets and use -
to find the difference:
>>> list1 = [(12, 'Text1'), (123, 'Text2'), (22, 'Text3')]
>>> list2 = [('22', 'company1'), ('232', 'company2'), ('233', 'company3')]
>>> s1 = set(x for x, _ in list1)
>>> s2 = set(int(x) for x, _ in list2)
>>> s1 - s2
{123, 12}
>>> s2 - s1
{232, 233}