I have this nested list and I need to loop through it in some way to merge inner lists that have common elements (where these elements are lists in themselves). Here is a simplified data that has the pattern of the existing original data:
data = [
[[0,1],[2,3],[4,5]],
[[2,3],[4,5]],
[[4,5]],
[[6,7],[8,9],[10,11]],
[[8,9],[10,11]],
[[10,11]],
[[12,13],[14,15],[16,17],[18,19],[20,21]],
[[14,15],[16,17],[18,19],[20,21]],
[[16,17],[18,19],[20,21]],
[[18,19],[20,21]],
[[20,21]]
]
I want to obtain a merged nested list as given given below:
merged = [
[[0,1],[2,3],[4,5]],
[[6,7],[8,9],[10,11]],
[[12,13],[14,15],[16,17],[18,19],[20,21]]
]
Below is what I have tried which unfortunately is not going beyong the second inner for loop and is returning the error AttributeError: 'int' object has no attribute 'values':
tmp = {}
for subl in original:
subl = list(set(subl)) # Eliminating duplicates by first converting to a set
subl = dict(zip(subl, range(len(subl)))) # Create dictionary from list
sublswitch = {y:x for x,y in subl.items()} # Swap dictionary key for values and vice versa
ii = 0
for key in sublswitch:
tmp.setdefault(key.values(), set()).add(list(key.values())[ii])
ii += 1
out = []
for k, v in tmp.items():
out.append([[k, i] for i in v])