I want to merge two lists which contain same number.
Each list contains unique number and never matches more than one row of the other list.
([2, 1, 3, 4, 5]
of listA only match a single row [1, 4, 3, 10]
of listB)
I wrote my code below in Python. Is there more efficient way to merge two lists? Though the sample code has only 3 length of list, I expect the length of the list is much more like 1k, so I want to find more efficient way.
listA = [
[2, 1, 3, 4, 5],
[50, 56, 60, 51],
[101, 112, 115, 110],
]
listB = [
[50, 63, 70],
[1, 4, 3, 10],
[120, 112, 116, 113],
]
mapA = {}
for idx, list_a in enumerate(listA):
for item in list_a:
mapA[item] = idx
result = []
for list_b in listB:
for item in list_b:
if item in mapA:
idx_of_listA = mapA[item]
nums = set(listA[idx_of_listA] + list_b)
result.append(nums)
break
print(result)
# [{70, 50, 51, 56, 60, 63}, {1, 2, 3, 4, 5, 10}, {101, 110, 112, 113, 115, 116, 120}]